SQL for Beginners: Commands, Joins and Practice Questions With Examples

SQL, which stands for Structured Query Language, is the standard language for working with data stored in databases. Banks, shopping apps, schools, hospitals and almost every large company store information in databases, and SQL lets you read, add, change and analyse that information. It is one of the most useful and beginner-friendly skills for anyone who wants a job in data, software or business analysis.

This guide teaches SQL in simple English with small examples. You will learn the basic commands, filters, sorting, grouping, joins and how to practise. Different database systems have small differences in syntax, so check the documentation of the system you use, and treat the examples as general patterns.

What Is a Database and a Table?

A database is an organised collection of data. Inside a database, data is stored in tables. A table looks like a spreadsheet with rows and columns. Each column has a name and a data type, such as number, text or date, and each row is one record. For example, a table called students may have columns such as roll_no, name, class and marks.

roll_no name class marks
1 Aarav 10 88
2 Meera 10 92
3 Rohan 9 75
4 Sana 9 81

Types of SQL Commands

Group Purpose Common commands
DDL (Data Definition) Create or change table structure CREATE, ALTER, DROP
DML (Data Manipulation) Add, change or delete data INSERT, UPDATE, DELETE
DQL (Data Query) Read data SELECT
DCL (Data Control) Give or remove permissions GRANT, REVOKE
TCL (Transaction Control) Manage changes COMMIT, ROLLBACK

As a beginner, spend most of your time on SELECT, because reading and analysing data is the most common task in jobs.

SELECT: Reading Data

The SELECT command chooses which columns to show, and FROM tells SQL which table to use.

SELECT name, marks
FROM students;

To see all columns, use an asterisk:

SELECT * FROM students;

WHERE: Filtering Rows

WHERE shows only the rows that match a condition.

SELECT name, marks
FROM students
WHERE marks >= 85;

You can combine conditions using AND, OR and NOT, and use operators such as =, <>, >, <, BETWEEN, IN and LIKE. For example, LIKE with a percent sign helps find names that start with a letter.

ORDER BY and LIMIT

ORDER BY sorts the result, and LIMIT (or a similar keyword depending on the system) shows only the first few rows.

SELECT name, marks
FROM students
ORDER BY marks DESC
LIMIT 3;

Aggregate Functions

Function What it does Example
COUNT Counts rows SELECT COUNT(*) FROM students;
SUM Adds values SELECT SUM(marks) FROM students;
AVG Finds the average SELECT AVG(marks) FROM students;
MAX and MIN Largest and smallest value SELECT MAX(marks) FROM students;

GROUP BY and HAVING

GROUP BY collects rows into groups so that you can summarise each group. HAVING filters those groups.

SELECT class, AVG(marks) AS avg_marks
FROM students
GROUP BY class
HAVING AVG(marks) > 80;

Remember that WHERE filters rows before grouping, while HAVING filters groups after grouping.

INSERT, UPDATE and DELETE

  • INSERT adds a new row: INSERT INTO students (roll_no, name, class, marks) VALUES (5, ‘Ishita’, 10, 90);
  • UPDATE changes existing data: UPDATE students SET marks = 95 WHERE roll_no = 2;
  • DELETE removes rows: DELETE FROM students WHERE roll_no = 4;

Always use WHERE with UPDATE and DELETE. Without it, the command changes or deletes every row in the table, which is a common beginner mistake.

Joins: Combining Tables

Real databases keep information in several tables and connect them using common columns. A join combines rows from two tables. Suppose you have a students table and a results table connected by roll_no.

Join type What it returns
INNER JOIN Rows that match in both tables
LEFT JOIN All rows from the left table and matching rows from the right
RIGHT JOIN All rows from the right table and matching rows from the left
FULL JOIN All rows from both tables, where supported
SELECT s.name, r.subject, r.score
FROM students s
INNER JOIN results r
ON s.roll_no = r.roll_no;

Primary Key and Foreign Key

A primary key is a column that uniquely identifies each row, such as roll_no. A foreign key is a column in one table that refers to the primary key of another table. These keys keep data connected and prevent mistakes, such as a result for a student who does not exist.

Practice Questions to Try

  1. Show the names of students who scored more than 80.
  2. Show the top three students by marks.
  3. Find the average marks for each class.
  4. Count how many students are in class 10.
  5. Update the marks of a student with roll number 3 to 78.
  6. Join students and results to show each student with their subject scores.

A 30-Day Practice Plan

Week Topics Practice
Week 1 Tables, SELECT, WHERE, ORDER BY Write 20 simple queries
Week 2 Aggregate functions, GROUP BY, HAVING Analyse a small sales dataset
Week 3 INSERT, UPDATE, DELETE, keys Create and modify your own tables
Week 4 Joins and subqueries Solve practice problems and mini projects

Common Mistakes to Avoid

  • Forgetting WHERE in UPDATE or DELETE
  • Mixing up WHERE and HAVING
  • Using SELECT * in large tables without need
  • Not using aliases in joins, which makes queries hard to read
  • Not checking the results against the data

SQL for Jobs and Interviews

SQL is required in roles such as data analyst, business analyst, backend developer and report developer. Interview questions usually test joins, grouping, filtering and basic subqueries. Practise by writing queries on sample data and explaining your logic. Pair SQL with Excel and a visualisation tool to become job-ready. See our data science career roadmap and the computer and IT courses section for the next steps.

Subqueries and Useful Keywords

A subquery is a query inside another query. For example, to find students whose marks are higher than the class average, you can use a subquery that calculates the average and then compare each student’s marks with it. Other useful keywords include DISTINCT to remove duplicate values, BETWEEN to filter ranges and IN to match a list of values. Learning these small tools makes your queries shorter and clearer.

SELECT name, marks
FROM students
WHERE marks > (SELECT AVG(marks) FROM students);

Read the query from the inside out: first the inner query finds the average, then the outer query uses it.

Designing Your Own Small Database

A good exercise is to design a database for a library. Create tables for books, members and loans. The books table stores the book ID, title and author. The members table stores the member ID and name. The loans table connects them with the book ID, member ID and dates. Then write queries to find which books a member has borrowed, which books are overdue and how many books each member has taken. This exercise teaches table design, keys and joins together.

Data Types and Constraints

When you create a table, each column must have a data type. Common types include integers for whole numbers, decimals for numbers with fractions, text types for names and date types for dates. Constraints are rules that protect your data. NOT NULL means a column cannot be empty, UNIQUE means values cannot repeat, PRIMARY KEY identifies rows and CHECK limits the allowed values, such as marks between 0 and 100. Using constraints early prevents wrong data from entering the database and saves time in cleaning later.

Constraint Purpose
NOT NULL The column must have a value
UNIQUE No two rows can have the same value
PRIMARY KEY Uniquely identifies each row
FOREIGN KEY Links to another table
DEFAULT Gives a value if none is provided

Tools You Can Use to Practise

You can practise SQL with a free local database system, a lightweight file-based database or a browser-based practice site. Choose one tool and install it carefully, then load a small sample dataset such as students, products or movies. Work through the same dataset using different queries to see how many questions you can answer with a few lines of SQL.

  • Start with datasets of 20 to 100 rows so you can check answers by eye.
  • Write each query on paper first, then run it.
  • Save your useful queries in a notes file.
  • Try to explain each query in one sentence.

Final Thoughts

SQL is easy to start and powerful to master. Practise every day with small questions, build a mini project such as a library or a school database and keep improving. A few weeks of steady work can make you comfortable with the basics.

Frequently Asked Questions

What is SQL?

SQL is a language used to store, read, change and analyse data in databases.

Is SQL easy for beginners?

Yes, the basic commands are easy to learn and the syntax is close to English.

What is the difference between SQL and MySQL?

SQL is the language, while MySQL is a database system that uses SQL.

What are the main SQL commands?

SELECT, INSERT, UPDATE, DELETE, CREATE and DROP are among the main commands.

What is a join?

A join combines rows from two or more tables based on a related column.

What is the difference between WHERE and HAVING?

WHERE filters rows before grouping, and HAVING filters groups after grouping.

What is a primary key?

It is a column that uniquely identifies each row in a table.

What is a foreign key?

It is a column that links one table to the primary key of another table.

Do I need to learn programming before SQL?

No. You can start SQL without any programming background.

How long does it take to learn SQL?

You can learn the basics in two to four weeks with regular practice.

Is SQL used in data science?

Yes, SQL is used to extract and prepare data for analysis.

How can I practise SQL for free?

Use free database tools and online practice platforms with sample tables.

About The Author

Knowledge Glow

I am Komal Gupta, the founder of Knowledge Glow, and my team and I aim to fuel dreams and help the readers achieve success. While you prepare for your competitive exams, we will be right here to assist you in improving your general knowledge and gaining maximum numbers from objective questions. We started this website in 2021 to help students prepare for upcoming competitive exams. Whether you are preparing for civil services or any other exam, our resources will be valuable in the process.

Related Posts