Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 3 out of 27 pages
Other

SQL Queries Handbook | 100 Most Used Queries for Exam & Interview Prep

Document preview thumbnail
Preview 3 out of 27 pages

Master relational databases with this comprehensive SQL Queries Handbook, featuring the 100 most frequently used queries across production, exams, and technical interviews! Whether you are preparing for a university Database Management Systems (DBMS) final exam, brushing up before a software engineering interview, or looking for a clean quick-revision guide, this document has you covered. It features full cross-compatibility notes highlighting syntax variations across MySQL, PostgreSQL, SQL Server, Oracle, and SQLite. Inside the Handbook: Section 1: SELECT & Filtering Basics – Explicit columns, WHERE, AND/OR precedence, DISTINCT, sorting with ORDER BY, pagination (LIMIT/OFFSET), range filters (BETWEEN), and wildcard pattern matching (LIKE). Section 2: Aggregate Functions & GROUP BY – Comprehensive guides to COUNT, SUM, AVG, MIN, MAX, grouping combinations, and generating automated subtotals/grand totals via ROLLUP and GROUPING SETS. Section 3: Combining Tables (JOINs) – Master INNER JOIN, LEFT/RIGHT OUTER JOIN, FULL OUTER JOIN solutions for MySQL, CROSS JOIN, Self Joins, and high-performance Anti Joins. Section 4: Subqueries & CTEs – In-depth usage of WHERE/FROM/SELECT subqueries, EXISTS vs. IN clauses, Correlated subqueries, and complex multi-chain or Recursive Common Table Expressions (CTEs). Section 5: Window Functions – Core analytical functions including ROW_NUMBER(), RANK(), DENSE_RANK(), calculating running totals or moving averages, and period-over-period row shifting using LAG() and LEAD(). Section 6: Built-in Functions – String manipulation (CONCAT, SUBSTRING, TRIM, padding), conditional logic (CASE WHEN), date arithmetic, type conversion (CAST), and safety functions like COALESCE or NULLIF to prevent division-by-zero errors. Section 7 & 8: DML & DDL (Schema Management) – Row modification workflows (INSERT, UPDATE with JOINs, DELETE vs. TRUNCATE), advanced UPSERT/MERGE queries, creating tables with strict data constraints (FOREIGN KEY CASCADE, CHECK), and database view design. Section 9 & 10: Transactions & Advanced Interview Classics – Understanding transactional ACID integrity (BEGIN, COMMIT, ROLLBACK, SAVEPOINT), optimizing querying performance using composite/unique INDEXES with EXPLAIN execution plans, implementing stored procedures, and resolving classic interview patterns (deduplicating tables, pivoting rows-to-columns). Includes a clean Quick Reference Cheat-Sheet summarizing the core execution rules and primary purposes of every major SQL clause at a single glance!

Content preview

SQL

Queries Handbook

100 Most Used Queries




■ SELECT & Filtering ■ JOINs ■ Aggregates



■ Subqueries ■ Window Functions ■ DDL & DML



■ Indexes & Views ■ Stored Procedures ■ Transactions




Exam Prep · Interview Ready · Quick Revision



Covers: MySQL · PostgreSQL · SQL Server · Oracle · SQLite syntax with notes on differences

,SQL Queries Handbook | 100 Most Used Queries Page 2




■ Section 1 — SELECT & Filtering Basics

#001
Select All Columns
Retrieve every column and row from a table. Use sparingly in production — fetching unused columns wastes
bandwidth.
SELECT *
FROM employees;
✎ Prefer listing explicit columns in production code. SELECT * is fine for quick exploration.

#002
Select Specific Columns
Retrieve only the columns you need — faster and cleaner.
SELECT first_name, last_name, salary
FROM employees;

#003
Filter with WHERE
Return only rows that match a condition.
SELECT *
FROM employees
WHERE department = 'Engineering';

#004
Multiple Conditions with AND / OR
Combine filters. AND requires both conditions; OR requires at least one.
SELECT *
FROM employees
WHERE department = 'Sales'
AND salary > 50000;
✎ Wrap OR conditions in parentheses to avoid precedence bugs: WHERE a AND (b OR c).

#005
NOT Operator
Exclude rows matching a condition.
SELECT *
FROM employees
WHERE NOT department = 'HR';

#006
DISTINCT — Remove Duplicates
Return unique values only.
SELECT DISTINCT department
FROM employees;
✎ DISTINCT applies across all selected columns, not just the first one.




Exam Prep · Interview Cracker · Quick Reference

, SQL Queries Handbook | 100 Most Used Queries Page 3




#007
ORDER BY — Sort Results
Sort results ascending (default) or descending.
SELECT first_name, salary
FROM employees
ORDER BY salary DESC;

#008
LIMIT / TOP — Restrict Rows
Return only the first N rows. Syntax varies by database.
-- MySQL / PostgreSQL / SQLite
SELECT * FROM employees LIMIT 10;
-- SQL Server
SELECT TOP 10 * FROM employees;
-- Oracle
SELECT * FROM employees WHERE ROWNUM <= 10;

#009
OFFSET — Pagination
Skip N rows before returning results. Combined with LIMIT for page-based results.
SELECT *
FROM employees
ORDER BY employee_id
LIMIT 10 OFFSET 20; -- page 3 (rows 21-30)
✎ Always include ORDER BY with OFFSET — without it, the 'skipped' rows are undefined.

#010
BETWEEN — Range Filter
Filter rows where a column value falls within an inclusive range.
SELECT *
FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31';
✎ BETWEEN is inclusive on both ends — equivalent to col >= low AND col <= high.

#011
IN — Match a List
Filter rows where a column matches any value in a list.
SELECT *
FROM employees
WHERE department IN ('Sales', 'Marketing', 'HR');

#012
NOT IN
Exclude rows matching any value in a list. Beware NULL values in the list — NOT IN returns no rows if the list
contains NULL.
SELECT *
FROM employees
WHERE department NOT IN ('Finance', 'Legal');
✎ If the subquery can return NULL, prefer NOT EXISTS over NOT IN.




Exam Prep · Interview Cracker · Quick Reference

Document information

Uploaded on
June 14, 2026
Number of pages
27
Written in
2025/2026
Type
Other
Person
Unknown
$8.49

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
3
Last sold
-


Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions