Learn SQL with Exercises: Master Query Writing Through Practice

Robert Jul 13, 2026

In today's data-driven world, SQL (Structured Query Language) is an indispensable skill. It's like learning a new language, but for computers and data. If you're eager to dive into the world of databases and querying, you're in the right place. Let's get started with learning SQL and throw in some exercises to help you master it.

Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts

SQL is used to manage and manipulate relational databases. It's not as daunting as it sounds, and with some practice, you'll be writing complex queries like a pro. So, let's roll up our sleeves and start with the basics.

SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻
SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻

Understanding SQL Basics

Before jumping into exercises, let's understand the building blocks of SQL. It's not much different from regular English, just a bit more structured.

Free SQL Courses for Beginners (2026 Complete Guide)
Free SQL Courses for Beginners (2026 Complete Guide)

Here's a simple SQL statement: SELECT name, age FROM users; In plain English, it means "Show me the names and ages of all the users."

Basic SQL Syntax

SQL Basics Every Beginner Must Learn | Ultimate SQL Cheat Sheet
SQL Basics Every Beginner Must Learn | Ultimate SQL Cheat Sheet

SQL statements always end with a semicolon (;), just like in English, you end a sentence with a period. You can ignore case, but keywords like SELECT, FROM, and WHERE are usually written in uppercase.

Here's a simple exercise to get you started: Try writing an SQL statement that selects the emails of all the users.

Selecting Data

5 SQL Aggregate Functions You'll Use Every Day
5 SQL Aggregate Functions You'll Use Every Day

Using the SELECT statement, you can choose which columns (or fields) to display. You can select one or multiple columns, and even use it to calculate fields on the fly using functions like SUM, AVG, COUNT, etc.

For example, SELECT name, 'User' as job FROM users; This will display a 'name' column and a 'job' column filled with 'User'. Give it a try and play around with this statement.

Filtering Data with WHERE

7 SQL Projects for Beginners | Build a Real Data Portfolio
7 SQL Projects for Beginners | Build a Real Data Portfolio

Now that you know how to select data, let's learn how to filter it. The WHERE clause lets you specify conditions that a row must meet to be included in the result.

Here's an exercise: Write a SQL statement that selects the names and emails of all users who are older than 30.

SQL Quick Notes Cheat Sheet | SQL Queries, Joins, Functions & Database Commands for Beginners
SQL Quick Notes Cheat Sheet | SQL Queries, Joins, Functions & Database Commands for Beginners
SQL LAG() Explained with Examples | Beginner SQL Window Function Tutorial
SQL LAG() Explained with Examples | Beginner SQL Window Function Tutorial
SQL Joins Explained with Examples | Beginner SQL Guide
SQL Joins Explained with Examples | Beginner SQL Guide
Learn SQL just by investing 30 minutes a day within 30 Days
Learn SQL just by investing 30 minutes a day within 30 Days
SQL Basics Tutorial for Beginners – Structured Query Language Commands, Syntax & Examples Guide
SQL Basics Tutorial for Beginners – Structured Query Language Commands, Syntax & Examples Guide
SQL developer Hand written Notes (Page-1)
SQL developer Hand written Notes (Page-1)
SQL Subqueries Explained with Examples | Beginner SQL Tutorial
SQL Subqueries Explained with Examples | Beginner SQL Tutorial
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
SQL Joins Explained 🚀 | Beginner Data Analyst SQL Guide
Sql Cheat Sheet, Learn Sql, Learn Web Development, Excel Tutorials, Sql Server, Career Options, Data Analyst, Data Scientist, Data Analysis
Sql Cheat Sheet, Learn Sql, Learn Web Development, Excel Tutorials, Sql Server, Career Options, Data Analyst, Data Scientist, Data Analysis
a sheet of paper that has some notes on it with words and symbols in the middle
a sheet of paper that has some notes on it with words and symbols in the middle
SQL RIGHT() Explained with Examples | Extract Characters from the Right in SQL Server
SQL RIGHT() Explained with Examples | Extract Characters from the Right in SQL Server
7 SQL QUERIES EVERY STUDENT SHOULD MASTER
7 SQL QUERIES EVERY STUDENT SHOULD MASTER
Learn SQL with Python: Beginner-Friendly Tutorial + Practice Exercise!
Learn SQL with Python: Beginner-Friendly Tutorial + Practice Exercise!
SQL NULLIF() Explained with Examples | Prevent Divide by Zero Errors in SQL
SQL NULLIF() Explained with Examples | Prevent Divide by Zero Errors in SQL
SQL LEAD() Explained with Examples | Beginner SQL Window Function Tutorial
SQL LEAD() Explained with Examples | Beginner SQL Window Function Tutorial
SQL JOINS Explained for Behinners
SQL JOINS Explained for Behinners
SQL CASE WHEN Explained with Examples | Beginner SQL Tutorial
SQL CASE WHEN Explained with Examples | Beginner SQL Tutorial
SQL SUM() Explained with Examples | Beginner SQL Tutorial
SQL SUM() Explained with Examples | Beginner SQL Tutorial
CQF - SQL
CQF - SQL

Comparison Operators

In SQL, you can use comparison operators like = (equal to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), and != (not equal to) to set conditions in the WHERE clause.

For example, SELECT name, email FROM users WHERE age > 30; This will display the names and emails of all users who are older than 30. Give it a try and see what happens when you change the condition.

Logical Operators

Sometimes, you might want to combine conditions using logical operators like AND, OR, and NOT. For example, SELECT name, email FROM users WHERE age > 30 AND country='USA'; This will display the names and emails of all users who are older than 30 and live in the USA.

Try writing a SQL statement that selects the names, ages, and countries of users who are younger than 25 OR live in Canada.

Ordering Data with ORDER BY

Once you have your results, you might want to order them in a specific way. That's where the ORDER BY clause comes in. It sorts the result-set by one or more columns.

For example, SELECT name, age, country FROM users ORDER BY age; This will display the names, ages, and countries of all users, sorted by age in ascending order. If you want to sort in descending order, you can use the DESC keyword, like this: ORDER BY age DESC;

Practice makes perfect. Try writing a SQL statement that selects the names and emails of all users, sorted by name in descending order.

And there you have it! You've covered the basics of SQL. You've learned how to select data, filter it, and order it. Remember, the best way to learn is by doing. Keep practicing, and soon you'll be ready to take on more complex tasks.

Now that you've got a solid foundation, why not explore more advanced topics like joins, aggregations, and subqueries? The world of SQL is vast, and there's always more to learn. Happy querying!