SQL Cheat Sheet for Beginners: Commands With Examples
Quick answer
One page of SQL commands with a purpose and a working example for each: clause order, WHERE vs HAVING, inner and outer joins, NULL handling and safe updates.
An SQL cheat sheet is a one-page list of the commands you use most: SELECT to read rows, WHERE to filter, ORDER BY to sort, GROUP BY and HAVING to summarise, JOIN to combine tables, and CREATE TABLE, INSERT, UPDATE and DELETE to change data. This one gives a purpose and a copy-ready example for each, in PostgreSQL-style syntax.
Key takeaways
- You write a query in a fixed order: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY.
- WHERE filters individual rows before grouping. HAVING filters groups after GROUP BY.
- An inner join keeps only matching rows. An outer join, such as LEFT JOIN, also keeps unmatched rows and fills the gaps with empty values.
- Test for missing values with IS NULL, never with = NULL.
On this page:
- In what order do SQL clauses go?
- How do you create a table and insert rows?
- How do you read and filter data?
- How do you summarise with GROUP BY and HAVING?
- How do joins work?
- How do you update and delete rows?
- Which mistakes trip up beginners?
- Frequently asked questions
| Position | Clause | What it does |
|---|---|---|
| 1 | SELECT | Chooses the columns or expressions to return |
| 2 | FROM | Names the table, or joins several |
| 3 | WHERE | Filters rows before grouping |
| 4 | GROUP BY | Forms groups so aggregates can summarise them |
| 5 | HAVING | Filters groups after aggregation |
| 6 | ORDER BY | Sorts the final result |
The clause order is standard SQL knowledge rather than a quote from one page. You can confirm it by running a query with the clauses in the wrong order and reading the error. All examples use PostgreSQL-style syntax, following the current PostgreSQL documentation. We ran the example queries on a SQLite engine to check the results, and we did not run them on every database, so test them on yours. For a comparison, read PostgreSQL vs MySQL.
In what order do SQL clauses go?
Write them in the order shown in the table above. A reliable mental model is to read a query as: pick a table, filter rows, group them, filter the groups, choose columns and sort. If a query fails, check the order of your clauses before anything else.
How do you create a table and insert rows?
Use CREATE TABLE with the table name and each column with its type, then INSERT to add rows. The PostgreSQL tutorial says you create a new table by specifying the table name, along with all column names and their types, and that INSERT is used to populate a table with rows.
These examples come from the PostgreSQL tutorial on creating a table and its page on populating a table:
CREATE TABLE weather (city varchar(80), temp_lo int, temp_hi int, prcp real, date date);
INSERT INTO weather VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');
INSERT INTO weather (city, temp_lo, temp_hi, prcp, date)
VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');The first INSERT lists values in column order. The second names the columns, which is safer because it keeps working if the column order changes. Text and dates go inside single quotes. Check data types such as real before using the same statements in another database. For more table-building detail, see creating SQL tables.
How do you read and filter data?
Use SELECT to retrieve data, WHERE to filter rows and ORDER BY to sort. The PostgreSQL tutorial on querying a table says an SQL SELECT statement is used to retrieve data from a table.
SELECT * FROM weather WHERE city = 'San Francisco' AND prcp > 0.0;
SELECT * FROM weather ORDER BY city;Both queries are from that tutorial. The first returns only San Francisco rows with rain. The second returns every row sorted by city. Add DESC after a column in ORDER BY to sort high to low.
How do you summarise with GROUP BY and HAVING?
Use an aggregate function with GROUP BY to compute one result per group, then HAVING to filter the groups. The PostgreSQL tutorial on aggregates says an aggregate function computes a single result from multiple input rows, and lists count, sum, avg, max and min.
SELECT city, count(*), max(temp_lo)
FROM weather
GROUP BY city;
SELECT city, count(*), max(temp_lo)
FROM weather
GROUP BY city
HAVING max(temp_lo) < 40;Both queries come from the tutorial. The second keeps only cities whose highest low temperature is under 40. With just the two San Francisco rows inserted above it returns no rows, because that city's highest low is 46. You cannot use WHERE for that, because the aggregate does not exist until grouping has happened.
Here is the same idea with a made-up students table. We use illustrative data: Asha, Ravi, Meera and Kabir with marks 82, 74, 91 and 67.
CREATE TABLE courses (course_id INT PRIMARY KEY, title VARCHAR(50));
CREATE TABLE students (id INT PRIMARY KEY, name VARCHAR(50), course_id INT, marks INT);
INSERT INTO courses VALUES (1, 'Python'), (2, 'SQL'), (3, 'Excel');
INSERT INTO students VALUES
(1, 'Asha', 1, 82), (2, 'Ravi', 2, 74), (3, 'Meera', NULL, 91), (4, 'Kabir', 1, 67);
SELECT course_id, count(*) AS n, avg(marks) AS avg_marks
FROM students
WHERE course_id IS NOT NULL
GROUP BY course_id
HAVING count(*) > 1
ORDER BY course_id;It returns one row: course 1, two students, an average of 74.5. Your database may display the average with more decimal places than the 74.5 we saw in SQLite. WHERE removed Meera's row first, then GROUP BY formed groups, and HAVING kept only the group with more than one student.
How do joins work?
A join combines rows from two tables using a matching condition. The PostgreSQL tutorial says queries that access multiple tables, or multiple instances of the same table, at one time are called join queries. See the tutorial on joins for its weather and cities example.
SELECT s.name, c.title
FROM students s
JOIN courses c ON s.course_id = c.course_id;
SELECT s.name, c.title
FROM students s
LEFT JOIN courses c ON s.course_id = c.course_id;The first, an inner join, returns Asha with Python, Ravi with SQL and Kabir with Python. Meera is missing because her course_id is NULL and matches nothing. The second, a left outer join, also returns Meera, with an empty title. The tutorial describes an outer join as substituting empty values for the missing table's columns when no matching row is found, and calls the joins before it inner joins. Our guide to SQL joins goes deeper.
How do you update and delete rows?
Use UPDATE with SET and DELETE FROM, and always include a WHERE clause. Without WHERE, the statement affects every row in the table.
UPDATE students SET marks = 70 WHERE id = 4;
DELETE FROM students WHERE id = 3;After running both, Kabir's marks are 70 and Meera's row is gone. Run a SELECT with the same WHERE condition first, to see which rows you are about to change.
Which mistakes trip up beginners?
These are common beginner mistakes. The list is ours, not taken from a single source.
- Putting HAVING before GROUP BY. Follow the clause order in the table.
- Comparing NULL with =.
WHERE course_id = NULLreturns no rows. UseWHERE course_id IS NULL, which returns Meera. - Forgetting quotes around text. Write
'Pune', notPune. - Running UPDATE or DELETE without WHERE. Test with SELECT first.
- Using WHERE on an aggregate. Use HAVING.
Messy source data causes many of these errors, so read data cleaning techniques too. When you are ready to be tested, try SQL interview questions and answers, and see how SQL compares in SQL vs Python for data analysts.
Next step
Create the two student tables above, run every query on this page and change one value at a time to predict the output. If you are learning SQL for data analysis, the Certification Program in Data Analytics with AI and Gen AI from E&ICT Academy, IIT Roorkee runs for 6 months. For syntax details, the PostgreSQL tutorial pages cited here are the reference to bookmark.
Frequently asked questions
What are the basic SQL commands for beginners?
Start with SELECT to read data, WHERE to filter, ORDER BY to sort, GROUP BY and HAVING to summarise, JOIN to combine tables, and CREATE TABLE, INSERT, UPDATE and DELETE to define and change data.
What is the difference between WHERE and HAVING?
WHERE filters individual rows before grouping. HAVING filters groups after GROUP BY, so it can use aggregates such as max(temp_lo) or count(*). Use WHERE for row conditions and HAVING for group conditions.
What is the order of clauses in a SELECT statement?
Write SELECT, FROM, WHERE, GROUP BY, HAVING and ORDER BY, in that order. Putting them in another order gives a syntax error, which is a quick way to confirm the rule on your database.
What is an inner join versus an outer join?
An inner join returns only rows with a match in both tables. An outer join, such as LEFT JOIN, also returns unmatched rows from one side and fills the other table's columns with empty values.
How do I create a table and insert rows?
Use CREATE TABLE with the table name, column names and types, then INSERT INTO with VALUES. Naming the columns in the INSERT is safer than relying on column order.
Does this work in MySQL too?
Much of it is standard SQL, but we have not verified every example in MySQL. Data types such as real and some syntax can differ, so test each query on your own database.