Resources / Coding
๐Ÿ’Ž PremiumCodingโฑ 21 min read

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.

๐Ÿ’ก How long this takes

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

๐Ÿ“– SQL

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.

๐Ÿ“– Table, row, column, primary key, foreign key

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

idnamecityjoined
1AshaHyderabad2024-01-10
2RaviBengaluru2024-02-05
3MeenaChennai2024-03-12
4KiranHyderabad2024-04-01
5FarhanPune2024-05-20

orders

idcustomer_idrestaurantamountorder_date
1011Paradise4502024-06-01
1021Domino's3202024-06-03
1032Meghana6802024-06-03
1043Paradise2502024-06-04
1052Domino's5402024-06-10
1064Meghana9002024-07-02
1071Meghana3002024-07-05

employees (the delivery company's staff)

idnamedeptsalarymanager_id
1SaraTech120000NULL
2OmarTech900001
3NishaTech900001
4VikramOps700001
5LeelaOps750004
6ArjunSales600004
๐Ÿ“ Words you will hear

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 โ†’
๐Ÿ”ฅJoin the free 30-Day Placement ChallengeOne resource and one task a day โ€” from resume to offer.