SQL cheat sheet + 25 interview queries
JOINs, GROUP BY, window functions and the 25 queries companies ask freshers, with answers.
How to use this guide
This guide takes you from the very first query to the 25 questions interviewers ask most. Follow the sections in order โ each one uses the same small food-delivery database, so every example builds on the last.
For every concept you get: a plain explanation โ a real-life example โ the query โ its output โ the mistake people make โ what interviewers ask. Try each query yourself in any free SQL playground before you read the output.
About 6โ8 hours spread over a week. Section 1โ5 on day 1โ2, joins and grouping on day 3โ4, window functions on day 5, the 25 interview queries on day 6โ7.
1. The database we use
Definition: SQL (Structured Query Language) is the standard declarative language for defining, querying and changing data in a relational database.
How it works: You describe what you want, not how to get it; the database's query optimiser decides the fastest way. SQL has sub-languages: DDL (CREATE, ALTER, DROP โ structure), DML (SELECT, INSERT, UPDATE, DELETE โ data), DCL (GRANT, REVOKE โ permissions) and TCL (COMMIT, ROLLBACK โ transactions).
Example: SELECT name FROM customers WHERE city = 'Hyderabad'; asks for names of Hyderabad customers.
Definition: A table (relation) stores data about one kind of thing in rows (records/tuples) and columns (attributes). A primary key is a column (or set of columns) whose value uniquely identifies each row and can never be NULL. A foreign key is a column in one table that references the primary key of another table, enforcing that the linked row exists.
How it works: The database rejects a duplicate primary key, and rejects an orders.customer_id that doesn't exist in customers.id (referential integrity).
Example: customers.id = 1 is Asha; orders.customer_id = 1 links three orders to her.
๐ง In simple words
Imagine a food-delivery app like Swiggy or Zomato. It stores data in tables:
customers
| id | name | city | joined |
|---|---|---|---|
| 1 | Asha | Hyderabad | 2024-01-10 |
| 2 | Ravi | Bengaluru | 2024-02-05 |
| 3 | Meena | Chennai | 2024-03-12 |
| 4 | Kiran | Hyderabad | 2024-04-01 |
| 5 | Farhan | Pune | 2024-05-20 |
orders
| id | customer_id | restaurant | amount | order_date |
|---|---|---|---|---|
| 101 | 1 | Paradise | 450 | 2024-06-01 |
| 102 | 1 | Domino's | 320 | 2024-06-03 |
| 103 | 2 | Meghana | 680 | 2024-06-03 |
| 104 | 3 | Paradise | 250 | 2024-06-04 |
| 105 | 2 | Domino's | 540 | 2024-06-10 |
| 106 | 4 | Meghana | 900 | 2024-07-02 |
| 107 | 1 | Meghana | 300 | 2024-07-05 |
employees (the delivery company's staff)
| id | name | dept | salary | manager_id |
|---|---|---|---|---|
| 1 | Sara | Tech | 120000 | NULL |
| 2 | Omar | Tech | 90000 | 1 |
| 3 | Nisha | Tech | 90000 | 1 |
| 4 | Vikram | Ops | 70000 | 1 |
| 5 | Leela | Ops | 75000 | 4 |
| 6 | Arjun | Sales | 60000 | 4 |
A table is like an Excel sheet. A row is one record (one order). A column is one fact (amount). The primary key (id) is unique for every row. A foreign key (orders.customer_id) points to a primary key in another table.
๐ Read the full document
This is a premium document. Log in and unlock it on ManiaJobs โ or catch it when it's free for 7 days after we post it on Instagram.
Open the full document โ