---
tags:
- devops
- l1
- flashcard-deck
- sql
---
<!-- wiki:breadcrumb:start -->
[Portal](../../../../library/portal/index.md) | **Level:** [L1: Foundations](../../../../library/portal/levels.md) | **Topics:** [SQL](../../../../library/portal/topics.md) | **Domain:** DevOps & Tooling
<!-- wiki:breadcrumb:end -->

id	category	difficulty	tags	question	answer	source_path
sql/a1b2c3d4e5f6	sql	easy	sql,select,basics	What type of language is SQL?	"SQL is declarative — you describe the result you want, not a step-by-step procedure. You say ""give me all orders over $100"" and the engine decides how to retrieve them. This means the database optimizer chooses the execution plan, which is why understanding indexes and EXPLAIN matters for performance.\n\nRemember: ""JOIN = combine rows from two tables."" INNER = only matches, LEFT = all from left + matches, RIGHT = all from right, FULL = all from both.\n\nName origin: SQL was originally called SEQUEL (Structured English Query Language) at IBM in 1974, later shortened to SQL for trademark reasons.\n\nFun fact: SQL is pronounced both ""S-Q-L"" and ""sequel"" — both are accepted. ISO standard says ""S-Q-L."""	zines/become.a.select.star.cleaned.notes
sql/b2c3d4e5f6a7	sql	easy	sql,where,filtering	What does the WHERE clause filter?	"Individual rows before any grouping occurs. WHERE runs early in the logical processing order (right after FROM), so you cannot use aggregate functions like COUNT or SUM in WHERE — those do not exist yet. Example: WHERE status = 'active' filters row by row.\n\nRemember: ""WHERE filters rows, HAVING filters groups."" HAVING comes after GROUP BY and can use aggregate functions.\n\nAnalogy: WHERE is like a bouncer at the door — it checks each row individually before letting it into the party (grouping)."	zines/become.a.select.star.cleaned.notes
sql/c3d4e5f6a7b8	sql	easy	sql,having,filtering	What does the HAVING clause filter?	"Groups after aggregation (e.g., after GROUP BY). Use HAVING for conditions on aggregate results: HAVING COUNT(*) > 5 keeps only groups with more than 5 rows. Common gotcha: putting aggregate conditions in WHERE instead of HAVING — the query will error because aggregates do not exist at WHERE time.\n\nRemember: ""WHERE = row filter (before GROUP BY). HAVING = group filter (after GROUP BY)."" This is the #1 SQL interview question."	zines/become.a.select.star.cleaned.notes
sql/d4e5f6a7b8c9	sql	medium	sql,count,aggregation	What is the difference between COUNT(*) and COUNT(col)?	COUNT(*) counts all rows including those with NULLs — it counts row existence. COUNT(col) counts only rows where that column is not NULL. Gotcha: COUNT(col) on a column with many NULLs will return a much smaller number than COUNT(*), which can silently produce wrong percentages or averages.\n\nGotcha: COUNT(DISTINCT col) counts unique non-NULL values. Often confused with COUNT(col) which counts all non-NULL values including duplicates.	zines/become.a.select.star.cleaned.notes
sql/e5f6a7b8c9d0	sql	medium	sql,null,comparison	"Why is ""x = NULL"" wrong in SQL?"	"NULL represents unknown, not a value. Comparing anything to NULL with = yields NULL (unknown), not TRUE or FALSE. You must use IS NULL or IS NOT NULL instead. Even NULL = NULL returns NULL. This three-valued logic (TRUE, FALSE, NULL) is one of the most common sources of subtle SQL bugs.\n\nRemember: ""NULL is not a value, it\'s the absence of a value."" Use IS NULL, never = NULL. This three-valued logic trips up even experienced developers."	zines/become.a.select.star.cleaned.notes
sql/f6a7b8c9d0e1	sql	hard	sql,execution-order,logical-order	What is the logical processing order of a SQL SELECT statement?	"FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY -> LIMIT. This differs from the written order which starts with SELECT. Understanding this order explains why you cannot use a SELECT alias in WHERE (WHERE runs first), why HAVING can reference aggregates (GROUP BY already ran), and why ORDER BY can use aliases (it runs after SELECT).\n\nRemember: ""FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT."" Mnemonic: Friendly Whales Gather Happily, Singing On Lakes."	zines/become.a.select.star.cleaned.notes
sql/a7b8c9d0e1f2	sql	easy	sql,order-by,sorting	What does ORDER BY do?	Sorts the final result rows by specified column(s), ASC (default) or DESC. Without ORDER BY, row order is undefined — do not rely on insertion order. You can sort by column position (ORDER BY 2) or expressions. Gotcha: ORDER BY on large result sets without LIMIT can be expensive; ensure the column is indexed for best performance.\n\nGotcha: Without ORDER BY, SQL does not guarantee row order — even if results appear sorted, the next execution may differ.	zines/become.a.select.star.cleaned.notes
sql/b8c9d0e1f2a3	sql	hard	sql,where,having,difference	Why can't you use an aggregate function in a WHERE clause?	"WHERE filters individual rows before GROUP BY runs, so aggregate values (COUNT, SUM, AVG) do not exist yet. Use HAVING instead, which runs after grouping. Example of the error: WHERE COUNT(*) > 5 fails. Correct form: HAVING COUNT(*) > 5. This distinction trips up even experienced developers who do not internalize the logical execution order.\n\nRemember: ""WHERE runs before GROUP BY, so aggregates don\'t exist yet."" Use HAVING for aggregate conditions."	zines/become.a.select.star.cleaned.notes
sql/c9d0e1f2a3b4	sql	easy	sql,group-by,aggregation	What does GROUP BY do?	"Groups rows sharing the same values in specified columns, enabling aggregate functions (COUNT, SUM, AVG) to operate per group. Example: SELECT dept, COUNT(*) FROM employees GROUP BY dept. Every non-aggregate column in SELECT must appear in GROUP BY — otherwise the database does not know which row's value to show for the group.\n\nRemember: ""Every non-aggregate column in SELECT must be in GROUP BY."" This rule ensures unambiguous results — the database knows which value to show for each group."	zines/become.a.select.star.cleaned.notes
sql/d0e1f2a3b4c5	sql	medium	sql,aggregation,functions	Name five common SQL aggregate functions.	COUNT() — row count, SUM() — total, AVG() — mean, MIN() — smallest, MAX() — largest. All except COUNT(*) ignore NULLs. Gotcha: AVG of a column with NULLs divides by the non-NULL count, not total rows — this can skew results. Use COALESCE(col, 0) if NULLs should count as zero.\n\nGotcha: AVG ignores NULLs — if 5 of 10 rows are NULL, AVG divides by 5, not 10. Use COALESCE(col, 0) if NULLs should count as zero.	zines/become.a.select.star.cleaned.notes
sql/e1f2a3b4c5d6	sql	hard	sql,null,three-valued-logic	What does NULL = NULL evaluate to in SQL?	It evaluates to NULL (unknown), not TRUE. This means WHERE col = NULL returns no rows. Joins on nullable columns can silently drop rows. To compare NULLs safely use IS NULL, IS NOT NULL, or COALESCE. In CASE expressions, use WHEN col IS NULL, not WHEN col = NULL. This is one of SQL's most misunderstood behaviors.\n\nFun fact: This behavior is defined by the SQL standard\'s three-valued logic (TRUE, FALSE, UNKNOWN). NULL = NULL yields UNKNOWN, not TRUE.	zines/become.a.select.star.cleaned.notes
sql/f2a3b4c5d6e7	sql	hard	sql,execution-order,alias	Why can't you reference a column alias from SELECT in the WHERE clause?	Because WHERE is processed before SELECT in the logical execution order (FROM -> WHERE -> GROUP BY -> HAVING -> SELECT). The alias does not exist yet when WHERE runs. Workaround: repeat the expression in WHERE, or use a subquery/CTE where the alias is defined in the inner query and filtered in the outer query.\n\nGotcha: Some databases (MySQL) allow alias references in HAVING but not WHERE. PostgreSQL follows the standard strictly.	zines/become.a.select.star.cleaned.notes
sql/a3b4c5d6e7f8	sql	easy	sql,from,tables	What does the FROM clause specify?	"The source table(s) from which data will be retrieved. You can join multiple tables (INNER JOIN, LEFT JOIN, etc.), use subqueries as derived tables, or reference CTEs. FROM runs first in the logical order, which is why every other clause operates on the rows it produces.\n\nRemember: ""EXPLAIN shows the query plan."" Look for full table scans (bad), index scans (good), and estimated row counts.\n\nUnder the hood: FROM is processed first in the logical order. This is why you can reference table columns in WHERE — the tables are already resolved."	zines/become.a.select.star.cleaned.notes
sql/b4c5d6e7f8a9	sql	medium	sql,best-practices,debugging	Why should you inspect row-level data before writing a GROUP BY query?	"To verify you understand the data shape — check for unexpected NULLs, duplicates, or data quality issues that would silently produce wrong aggregates. Run SELECT * with a LIMIT first. A GROUP BY on dirty data can produce counts that look plausible but are wrong, which is worse than an obvious error.\n\nRemember: ""SELECT * LIMIT 10 before GROUP BY."" Always inspect raw data first to catch NULLs, duplicates, and data quality issues."	zines/become.a.select.star.cleaned.notes
sql/c5d6e7f8a901	sql	hard	sql,null,aggregation	How do NULLs affect aggregate functions like SUM and AVG?	Most aggregates (SUM, AVG, MIN, MAX) silently ignore NULL values. COUNT(col) ignores NULLs; only COUNT(*) counts all rows. The danger: AVG divides by non-NULL count, so a column with 10 rows where 5 are NULL computes the average of only 5 values. Use COALESCE(col, 0) if NULLs should be treated as zero to avoid skewed results.\n\nGotcha: SUM of all NULLs returns NULL, not 0. COUNT(*) of an empty table returns 0, but SUM returns NULL. Use COALESCE(SUM(col), 0) to avoid surprises.	zines/become.a.select.star.cleaned.notes

<!-- wiki:related:start -->
---

## Wiki Navigation

### Related Content

- Databases Flashcards *(CLI)* (flashcard_deck, L1) — SQL
- [SQL Fundamentals](../../../../library/topics/sql-fundamentals/index.md) (Topic Pack, L0) — SQL

<!-- wiki:related:end -->
