I WILL CONQUER SQL

0.0(0)
studied byStudied by 1 person
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/104

flashcard set

Earn XP

Description and Tags

I WILL GET THE JOB I WANT

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

105 Terms

1
New cards

SELECT column_name

Shows the column(s) you want to see

2
New cards

SELECT *

Shows all columns

3
New cards

FROM table_name

Specifies which table to pull data from

4
New cards

WHERE condition

Filters rows based on a condition

5
New cards

DISTINCT column_name

Removes duplicate values

6
New cards

AS alias_name

Renames a column or table temporarily

7
New cards

AND

Both conditions (in WHERE) must be true

8
New cards

OR

At least one condition (in WHERE) must be true

9
New cards

NOT

Reverses a condition

10
New cards

N (a, b, c)

Checks if value matches any in a list

11
New cards

BETWEEN a AND b

Checks if value falls in a range (inclusive)

12
New cards

LIKE 'pattern'

Pattern matching (% = any characters, _ = one character)

13
New cards

IS NULL

Checks for missing values

14
New cards

IS NOT NULL

Checks for non-missing values

15
New cards

COALESCE(a, b)

Returns first non-NULL value

16
New cards

COUNT(column)

Counts number of rows (ignores NULLs)

17
New cards

COUNT(*)

Counts all rows

18
New cards

SUM(column)

Adds values

19
New cards

AVG(column)

Calculates average

20
New cards

MIN(column)

Finds smallest value

21
New cards

MAX(column)

Finds largest value

22
New cards

GROUP BY column

Groups rows for aggregate functions

23
New cards

HAVING condition

Filters grouped results (used after GROUP BY)

24
New cards

ORDER BY column ASC

Sorts results ascending

25
New cards

ORDER BY column DESC

Sorts results descending

26
New cards

LIMIT n

Returns only n rows

27
New cards

JOIN

Returns only matching rows. Aka INNER JOIN.

28
New cards

LEFT JOIN

Returns all rows from left table + matches

29
New cards

RIGHT JOIN

Returns all rows from right table + matches

30
New cards

ON condition

Defines how tables are joined

31
New cards

SELECT ... FROM (subquery)

Query inside another query

32
New cards

WHERE column IN (subquery)

Filters using results from another query

33
New cards

WHERE ≠ HAVING

WHERE filters rows, HAVING filters aggregates

34
New cards

NULL ≠ 'null'

Use IS NULL, never = NULL

35
New cards

COUNT(column) ≠ COUNT(*)

Column ignores NULLs, * does not

36
New cards

Error: WHERE COUNT(*) > 5

Aggregate functions cannot be used in WHERE

Use HAVING instead: HAVING COUNT(*) > 5

37
New cards

If you use GROUP BY, every column in SELECT must be…

  • In GROUP BY, or

  • Inside an aggregate function

38
New cards

WHERE vs HAVING

WHERE: Filters rows before grouping

HAVING: Filters groups after aggregation

WHERE age > 18       -- raw data
HAVING COUNT(*) > 5  -- aggregated data

39
New cards

NULL is not equal to anything — even itself.

NULL = NULL     -- ❌ false
NULL != 2       -- ❌ unknown

40
New cards

DISTINCT applies to the entire row, not just one column (unless only one column is selected).

SELECT DISTINCT a, b

Removes duplicate (a, b) pairs

41
New cards

Conditions in WHERE can turn a LEFT JOIN into an INNER JOIN

LEFT JOIN Orders o ON c.id = o.customer_id
WHERE o.id IS NOT NULL   -- removes unmatched rows

42
New cards

AND has higher priority than OR

a = 1 OR b = 2 AND c = 3 -- is the same as
a = 1 OR (b = 2 AND c = 3)

Use parentheses to be safe.

43
New cards

BETWEEN is inclusive

BETWEEN 1 AND 5 i ncludes 1 and 5

44
New cards

% → any number of characters
_ → exactly one character

LIKE 'A%'   -- starts with A
LIKE '%A'   -- ends with A
LIKE '_A%'  -- A is second character

45
New cards

Column aliases cannot be used in WHERE

SELECT salary * 12 AS annual
WHERE annual > 50000   -- ❌ invalid

Use subquery or HAVING

46
New cards

ORDER BY runs after SELECT, so it can use aliases

SELECT salary * 12 AS annual
ORDER BY annual   -- ✅ valid

47
New cards

LIMIT Applied after sorting

ORDER BY salary DESC
LIMIT 5

Top 5 highest salaries

48
New cards

Order of SQL execution

FROM → JOIN → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT

49
New cards
FROM A
LEFT JOIN B ON ...

  • A = left table

  • B = right table

50
New cards

Do I want to keep rows from this table even if there is NO matching row in the other table?

  • YESLEFT JOIN

  • NOINNER JOIN

51
New cards

EXISTS Format

SELECT c.name
FROM Customers c
WHERE NOT EXISTS (
    SELECT 1
    FROM Orders o
    WHERE o.customer_id = c.id
);

52
New cards

CASE WHEN format

CASE
    WHEN o.amount >= 100 THEN 'High'
    WHEN o.amount >= 50 THEN 'Medium'
    ELSE 'Low'
END AS order_size;

53
New cards

Full query format template

SELECT
    c.id,
    c.name,
    COUNT(o.id) AS order_count
FROM Customers c
LEFT JOIN Orders o
    ON c.id = o.customer_id
WHERE
    c.active = 1
GROUP BY
    c.id,
    c.name
HAVING COUNT(o.id) > 3
ORDER BY
    order_count DESC
LIMIT 5;

54
New cards

EXISTS

Checks whether a subquery returns at least one row. Returns TRUE or FALSE.

WHERE EXISTS (
    SELECT 1
    FROM Orders o
    WHERE o.customer_id = c.id
)

55
New cards

NOT EXISTS

Checks whether a subquery returns no rows.

56
New cards

CASE WHEN

Creates conditional logic in SQL (like if / else). Evaluates conditions top to bottom.

57
New cards

IF vs CASE WHEN

  • Use CASE WHEN for conditional logic in queries.

  • Use IF() only if you’re explicitly in MySQL and writing something quick.

58
New cards

PRIMARY KEY

Uniquely identifies each row in a table.
Cannot be NULL. No duplicates.

59
New cards

FOREIGN KEY

Column that references a primary key in another table.
Used to connect tables in joins.

60
New cards

INNER JOIN vs LEFT JOIN

INNER JOIN returns only matched rows.
LEFT JOIN preserves all rows from the left table.

61
New cards

LEFT JOIN + WHERE trap

Conditions on the joined table in WHERE can remove unmatched rows.
Use ON for joined-table filters.

62
New cards

ON vs WHERE

ON defines how tables match.
WHERE filters rows after the join.

63
New cards

NOT IN

Checks values not in a list or subquery.

Fails if subquery contains NULL.

64
New cards

EXISTS vs IN

EXISTS checks for presence of rows (safer with NULLs).
IN compares values directly.

65
New cards

Window function

Performs calculations across related rows without collapsing rows.

COUNT(*) OVER (PARTITION BY column)

66
New cards

GROUP BY vs WINDOW FUNCTION

GROUP BY collapses rows.
Window functions keep original rows.

67
New cards

CASE WHEN in aggregation

Used for conditional counts or sums.

SUM(CASE WHEN condition THEN 1 ELSE 0 END)

68
New cards

Conditional aggregation

Counts or sums based on conditions inside aggregates.

69
New cards

SELECT DISTINCT vs GROUP BY

DISTINCT removes duplicate rows.
GROUP BY aggregates data.

70
New cards

Ambiguous column

Occurs when column name exists in multiple tables.
Fix with table.column.

71
New cards

Alias best practice

Always alias tables in joins.
Use alias.column everywhere.

72
New cards

LIMIT without ORDER BY

Results are not guaranteed to be consistent.

73
New cards

TOP vs LIMIT

TOP → SQL Server
LIMIT → MySQL / PostgreSQL

74
New cards

COUNT(*) with LEFT JOIN

Counts all rows, including those with NULL joined values.

75
New cards

COUNT(column) with LEFT JOIN

Counts only non-NULL values from the joined table.

76
New cards

Scalar subquery

Subquery that returns exactly one value.

SELECT (SELECT MAX(salary) FROM Employees)

77
New cards

Correlated subquery

A subquery that uses values from the outer query and runs once per outer row. Like a join that runs one row at a time.

SELECT c.name
FROM Customers c
WHERE EXISTS (
    SELECT 1
    FROM Orders o
    WHERE o.customer_id = c.id
);

78
New cards

LENGTH(column)

Returns the number of characters in a string. Filter questions (“names longer than 10 chars”)

79
New cards

LOWER(column)

Converts text to lowercase.

80
New cards

UPPER(column)

Converts text to uppercase.

81
New cards

TRIM(column)

Removes leading and trailing spaces. Ex. “ Zoe “ becomes “Zoe”.

82
New cards

SUBSTRING(column, start, length)

Extracts part of a string.

83
New cards

CONCAT(a, b)

Joins strings together.

CONCAT(first_name, ' ', last_name)

84
New cards

CURRENT_DATE

Returns today’s date.

85
New cards

NOW()

Returns current date and time.

86
New cards

DATEDIFF(date1, date2)

Returns number of days between dates.

DATEDIFF(end_date, start_date)

Ex. “Users active in last 30 days”

87
New cards

YEAR(date)

Extracts year from a date.

88
New cards

MONTH(date)

Extracts month from a date.

89
New cards

ROUND(number, decimals)

Rounds a number. Ex. ROUND(price, 2)

90
New cards

ABS(number)

Returns absolute value. Ex. ABS(balance)

91
New cards

NULLIF(a, b)

Returns NULL if a = b, else returns a.

NULLIF(total, 0)

92
New cards

!= vs <>

Both mean “not equal.”

salary != 50000
salary <> 50000

93
New cards

LIKE vs =

= matches exact value

LIKE matches patterns

94
New cards

REPLACE(column, old, new)

Replaces all occurrences of a substring. Used for cleaning formatted text.

REPLACE(phone, '-', '')

95
New cards

CAST(value AS type)

Converts a value to a different data type.

CAST(price AS INT)

96
New cards

FLOOR()

rounds down

97
New cards

CEILING()

rounds up

98
New cards

ORDER BY multiple columns

Sort priority matters.

ORDER BY score DESC, name ASC

99
New cards

LIMIT vs. OFFSET

LIMIT = how many rows to return
OFFSET = how many rows to skip first

100
New cards

OFFSET format template

SELECT *
FROM Users
ORDER BY id
LIMIT 5 OFFSET 5;
  • Skip 5, take 5

  • Rows: 6–10