1/30
Looks like no tags are added yet.
INNER JOIN returns?
Only rows with a match in both tables
LEFT JOIN with an unmatched left row?
Row is kept; the right table's columns are NULL
A RIGHT JOIN B is the same rows as?
B LEFT JOIN A (only SELECT * column order can differ)
How to do FULL OUTER JOIN in MySQL?
A LEFT JOIN B … UNION … A RIGHT JOIN B (UNION, not UNION ALL, so matched rows aren't duplicated)
ON vs USING?
USING(col) needs a same-named column and shows it once in SELECT *; ON takes any condition (different names, <, AND)
Find users who never borrowed?
user LEFT JOIN borrow USING(user_id) WHERE borrow.user_id IS NULL (= NULL never works)
LEFT JOIN with a right-table condition in WHERE?
WHERE runs after the join and removes the NULL rows, so it acts like an INNER JOIN; put the condition in ON
user LEFT JOIN borrow then INNER JOIN book USING(book_id): who disappears?
Users who never borrowed (NULL book_id can't match); make the second join a LEFT JOIN too
After LEFT JOIN, COUNT(*) for a user with 0 borrows?
1 (the NULL-padded row still counts)
After LEFT JOIN, COUNT(borrow.borrow_id) for a user with 0 borrows?
0 (COUNT(column) skips NULL)
SUM over no matching rows?
NULL; use IFNULL(SUM(x), 0) to get 0
CROSS JOIN of 4 users and 5 books?
20 rows (m x n); a missing join condition does the same
Why does COUNT per genre come out too big after joining to borrow?
Joins multiply rows (a book borrowed 3 times appears 3 times); use COUNT(DISTINCT book.book_id)
Self join: crew member plus manager, keeping crew with no manager?
SELECT c.name, m.name FROM crew c LEFT JOIN crew m ON c.reports_to = m.crew_id
Self join: list each pair only once?
Add a.id < b.id (removes the (B,A) repeats and the (A,A) self-pairs)
A has 3 rows: can A LEFT JOIN B return more than 3?
Yes; at least 3, and more when a row of A matches several rows of B
SELECT genre_id, title, MAX(pages) … GROUP BY genre_id: what's wrong?
MAX gives the value, not its row; title isn't tied to the max (error or arbitrary value)
Join-to-max and correlated-subquery methods: limits?
They find only the single top row per group, return every tied row, and can't do top 2
PARTITION BY inside OVER()?
Restarts the calculation for each group
ORDER BY inside OVER()?
Sets the ranking order within each partition
ROW_NUMBER for 100, 90, 90, 80?
1, 2, 3, 4
RANK for 100, 90, 90, 80?
1, 2, 2, 4
DENSE_RANK for 100, 90, 90, 80?
1, 2, 2, 3
Which ranking functions can return more than N rows with <= N?
RANK and DENSE_RANK, because of ties; ROW_NUMBER always returns exactly N or fewer
Can you filter WHERE rn <= 2 in the same query that defines rn?
No; WHERE runs before window functions. Wrap the query in a subquery or CTE and filter outside it
Window function vs GROUP BY?
A window function keeps every row; GROUP BY collapses each group into one row
Percent of group total?
x / SUM(x) OVER (PARTITION BY g) * 100
Are book LEFT JOIN genre and genre RIGHT JOIN book (USING genre_id, selecting title and genre_name) equivalent?
Yes; both keep every book, with NULL genre_name where there's no match
Fix a query that has row_number() AS rn and WHERE rn <= 2 in the same SELECT
SELECT … FROM (SELECT …, ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY borrow_dt DESC) AS rn FROM …) t WHERE rn <= 2
Write: books borrowed per user, including users with none
SELECT user_name, COUNT(borrow.book_id) FROM user LEFT JOIN borrow USING(user_id) GROUP BY user_id, user_name
Two traps in "count per user, including zero" queries?
An inner join drops the zero users, and COUNT(*) shows 1 instead of 0