Databases Quiz #4

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/30

encourage image

There's no tags or description

Looks like no tags are added yet.

31 Terms

1
New cards

INNER JOIN returns?

Only rows with a match in both tables

2
New cards

LEFT JOIN with an unmatched left row?

Row is kept; the right table's columns are NULL

3
New cards

A RIGHT JOIN B is the same rows as?

B LEFT JOIN A (only SELECT * column order can differ)

4
New cards

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)

5
New cards

ON vs USING?

USING(col) needs a same-named column and shows it once in SELECT *; ON takes any condition (different names, <, AND)

6
New cards

Find users who never borrowed?

user LEFT JOIN borrow USING(user_id) WHERE borrow.user_id IS NULL (= NULL never works)

7
New cards

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

8
New cards

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

9
New cards

After LEFT JOIN, COUNT(*) for a user with 0 borrows?

1 (the NULL-padded row still counts)

10
New cards

After LEFT JOIN, COUNT(borrow.borrow_id) for a user with 0 borrows?

0 (COUNT(column) skips NULL)

11
New cards

SUM over no matching rows?

NULL; use IFNULL(SUM(x), 0) to get 0

12
New cards

CROSS JOIN of 4 users and 5 books?

20 rows (m x n); a missing join condition does the same

13
New cards

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)

14
New cards

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

15
New cards

Self join: list each pair only once?

Add a.id < b.id (removes the (B,A) repeats and the (A,A) self-pairs)

16
New cards

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

17
New cards

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)

18
New cards

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

19
New cards

PARTITION BY inside OVER()?

Restarts the calculation for each group

20
New cards

ORDER BY inside OVER()?

Sets the ranking order within each partition

21
New cards

ROW_NUMBER for 100, 90, 90, 80?

1, 2, 3, 4

22
New cards

RANK for 100, 90, 90, 80?

1, 2, 2, 4

23
New cards

DENSE_RANK for 100, 90, 90, 80?

1, 2, 2, 3

24
New cards

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

25
New cards

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

26
New cards

Window function vs GROUP BY?

A window function keeps every row; GROUP BY collapses each group into one row

27
New cards

Percent of group total?

x / SUM(x) OVER (PARTITION BY g) * 100

28
New cards

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

29
New cards

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

30
New cards

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

31
New cards

Two traps in "count per user, including zero" queries?

An inner join drops the zero users, and COUNT(*) shows 1 instead of 0