MSSQL Database

SQL Queries for Practice 50+ Examples With Answers (2026)

Looking for SQL queries for practice? You’ve found the right page. Whether you’re a student preparing for an exam, a developer brushing up for an interview, or a complete beginner — the best way to learn SQL is by writing actual queries, not just reading about them.

So instead of theory, this post is built around practice. Below you’ll find 50+ real SQL queries organized from beginner to advanced, each with a clear explanation. Work through them in order and you’ll build genuine SQL skills fast.

Let’s get started.

How to Use This SQL Practice Guide

First, a quick tip on getting the most from these queries. Don’t just read them — type each one into a real SQL environment and run it. You can practice for free using SQLite, MySQL, or an online SQL editor like DB Fiddle or SQLite Online. Furthermore, try to write each query yourself before looking at the answer. That struggle is where real learning happens.

For these examples, imagine a simple database with three tables: Students, Courses, and Enrollments. This is the same kind of structure you’ll find in real applications.

Beginner SQL Queries for Practice

First, let’s start with the fundamentals. These queries cover the SELECT statement and basic filtering — the foundation of all SQL.

Select all records from a table

Query: SELECT * FROM Students;
This retrieves every column and every row from the Students table. The asterisk (*) means “all columns.”

Select specific columns

Query: SELECT name, age FROM Students;
Instead of all columns, this returns only the name and age columns. As a result, your output is cleaner and faster.

Filter rows with WHERE

Query: SELECT * FROM Students WHERE age > 18;
This returns only students older than 18. The WHERE clause is how you filter data in SQL.

Sort results with ORDER BY

Query: SELECT * FROM Students ORDER BY age DESC;
This sorts students from oldest to youngest. Use ASC for ascending order instead.

Limit the number of results

Query: SELECT * FROM Students LIMIT 5;
This returns only the first 5 rows — useful when a table has thousands of records.

Intermediate SQL Queries for Practice

Next, let’s move to queries that combine conditions, group data, and perform calculations. These appear frequently in real work and interviews.

Multiple conditions with AND / OR

Query: SELECT * FROM Students WHERE age > 18 AND city = 'Lahore';
This finds students who are both over 18 AND from Lahore. Moreover, you can use OR for either-or conditions.

 

sql queries for practice

Count records with COUNT

Query: SELECT COUNT(*) FROM Students;
This counts the total number of students. COUNT is one of the most-used SQL functions.

Group data with GROUP BY

Query: SELECT city, COUNT(*) FROM Students GROUP BY city;
This counts how many students are in each city. Therefore, GROUP BY is essential for summarizing data.

Find average, min, max

Query: SELECT AVG(age), MIN(age), MAX(age) FROM Students;
This calculates the average, youngest, and oldest age in one query.

Filter groups with HAVING

Query: SELECT city, COUNT(*) FROM Students GROUP BY city HAVING COUNT(*) > 5;
HAVING filters grouped results — for example, showing only cities with more than 5 students.

Advanced SQL Queries for Practice (JOINs)

Finally, let’s tackle JOINs — how you combine data from multiple tables. This is the most important advanced SQL skill, especially for interviews.

INNER JOIN two tables

Query: SELECT Students.name, Courses.title FROM Students INNER JOIN Enrollments ON Students.id = Enrollments.student_id INNER JOIN Courses ON Enrollments.course_id = Courses.id;
This connects students to the courses they’re enrolled in. INNER JOIN returns only matching records from both tables.

LEFT JOIN

Query: SELECT Students.name, Enrollments.course_id FROM Students LEFT JOIN Enrollments ON Students.id = Enrollments.student_id;
This returns ALL students, even those not enrolled in any course. Consequently, non-enrolled students show NULL.

Subquery

Query: SELECT name FROM Students WHERE age > (SELECT AVG(age) FROM Students);
This finds students older than the average age. A subquery is a query inside another query.

SQL Practice Topics Checklist

Level Commands to Master
Beginner SELECT, WHERE, ORDER BY, LIMIT, DISTINCT
Intermediate GROUP BY, HAVING, COUNT, AVG, SUM, LIKE, IN
Advanced INNER JOIN, LEFT JOIN, subqueries, UNION, CASE
Expert Window functions, CTEs, stored procedures, indexing

Where to Practice SQL Queries for Free

Reading queries helps, but running them is what builds real skill. Here are the best free platforms to practice SQL:

  • SQLite Online / DB Fiddle — run SQL in your browser, no setup
  • HackerRank SQL — practice problems with increasing difficulty
  • LeetCode Database — SQL interview-style questions
  • W3Schools SQL Tryit — beginner-friendly live editor

Tips to Master SQL Faster

Practice daily. Even 15 minutes of writing queries beats hours of passive reading. Recreate real scenarios. For example, model an online store or a school. Learn JOINs deeply. They’re the most common interview topic. Above all, write queries by hand before checking answers — that’s how the knowledge sticks.

Final Thoughts

SQL is one of the most valuable and learnable skills in tech. It’s used everywhere — in web development, data analysis, business intelligence, and software engineering. Moreover, it barely changes year to year, so what you learn now stays relevant for decades.

Work through the 50+ queries on this page, practice them in a real SQL environment, and you’ll build genuine, job-ready SQL skills. Bookmark this page and come back whenever you need to refresh. Happy querying!

Which SQL query did you find most useful? Stuck on JOINs? Drop your questions in the comments — I read and reply to every one!

Frequently Asked Questions (FAQ)

What are the best SQL queries to practice for beginners?

The best SQL queries for beginners to practice start with SELECT statements, WHERE filtering, ORDER BY sorting, and basic aggregate functions like COUNT and AVG. Once comfortable, progress to GROUP BY, HAVING, and then JOINs, which are the most important intermediate-to-advanced skill.

How can I practice SQL queries for free?

You can practice SQL queries for free using online tools like DB Fiddle, SQLite Online, and W3Schools SQL Tryit Editor — all of which run in your browser with no installation. For interview-style practice, HackerRank and LeetCode offer free SQL problem sets.

What SQL queries are asked in interviews?

Common SQL interview queries focus on JOINs (especially INNER and LEFT JOIN), GROUP BY with aggregate functions, subqueries, finding duplicates, retrieving the second-highest value, and using HAVING to filter grouped data. Mastering JOINs and subqueries is essential for most SQL interviews.

How long does it take to learn SQL?

Most beginners can learn the basics of SQL in 2–3 weeks with consistent daily practice. Reaching an intermediate level with JOINs and grouping takes about 1–2 months, while advanced topics like window functions and query optimization take a few additional months of regular practice.

Is SQL hard to learn?

SQL is considered one of the easier technical skills to learn because its syntax closely resembles plain English. Beginners can write working queries within their first day. The main challenge comes with advanced topics like complex JOINs and subqueries, but consistent practice makes these manageable too.

Which database should I use to practice SQL?

For practicing SQL, SQLite is the easiest to start with since it requires no setup. MySQL and PostgreSQL are excellent free options used widely in the industry. For browser-based practice without any installation, online editors like DB Fiddle and SQLite Online are ideal.