SQL Quires Flashcards

(30 cards)

1
Q

What are the 3 questions every SQL query starts with?

A
  • What do you want to see? (SELECT)
  • Where does it live? (FROM / JOIN)
  • What rules apply? (WHERE)

These questions guide the structure of an SQL query.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does an INNER JOIN return?

A

Only rows that match in both tables (the overlap)

INNER JOIN is used to combine rows from two or more tables based on a related column.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does a LEFT JOIN return?

A

All rows from the left table + matches from the right; unmatched right rows become NULL

LEFT JOIN includes all records from the left table and matched records from the right table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does a RIGHT JOIN return?

A

All rows from the right table + matches from the left

RIGHT JOIN includes all records from the right table and matched records from the left table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does a FULL OUTER JOIN return?

A

All rows from both tables, matched where possible, NULL where not

FULL OUTER JOIN returns all records when there is a match in either left or right table records.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does a CROSS JOIN do?

A

Returns every combination of rows from table A × table B

CROSS JOIN produces a Cartesian product of the two tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the purpose of JOINs?

A

To combine tables side-by-side using matching columns (usually IDs)

JOINs are essential for relational database queries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does UNION do?

A

Stacks results vertically and removes duplicates

UNION combines the result sets of two or more SELECT statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does UNION ALL do?

A

Stacks results vertically but keeps duplicates

UNION ALL is used when you want to include all records, including duplicates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does INTERSECT return?

A

Only rows that appear in both queries

INTERSECT is used to find common records between two SELECT statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does EXCEPT return?

A

Rows from the first query that do not appear in the second (“A minus B”)

EXCEPT is used to find records in one query that are not present in another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is required for UNION / INTERSECT / EXCEPT to work?

A

Both queries must have the same number of columns and compatible data types

This ensures that the operations can be performed correctly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

In combined queries, which SELECT decides the column names?

A

The first SELECT

The column names in the result set are taken from the first query.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the WHERE clause do?

A

Filters rows based on conditions

WHERE is used to specify criteria for selecting records.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does AND do in a WHERE clause?

A

All conditions must be true

AND is used to combine multiple conditions in a WHERE clause.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does OR do in a WHERE clause?

A

At least one condition must be true

OR allows for flexibility in filtering records.

17
Q

Why use brackets with OR?

A

To ensure the logic evaluates as expected

Brackets clarify the order of operations in complex conditions.

18
Q

What does BETWEEN do?

A

Returns values within an inclusive range

BETWEEN is used to filter records based on a range of values.

19
Q

What does NOT do?

A

Shows everything except the specified condition

NOT is used to exclude certain records from the result set.

20
Q

What does a foreign key prevent?

A
  • Inserting children without parents
  • Deleting parents with children
  • Invalid updates

Foreign keys enforce referential integrity between tables.

21
Q

What is ON DELETE CASCADE?

A

Deleting a parent automatically deletes related child rows

This ensures that no orphaned records remain in the child table.

22
Q

What is ON UPDATE CASCADE?

A

Updating a parent key automatically updates child rows

This maintains consistency across related records.

23
Q

What problem does normalisation solve?

A
  • Duplicate data
  • Repeated information
  • Inconsistent updates
  • Messy tables

Normalisation organizes data to reduce redundancy.

24
Q

Core idea of normalisation?

A

Each fact is stored once; other tables reference it via IDs

This approach minimizes data duplication.

25
What is **1NF**?
* One value per cell * No repeating groups * Each row is a clear record * Table has a primary key ## Footnote 1NF is the first step in the normalization process.
26
Why is **1NF** important?
Prevents junk cells like "Laptop, Headphones" and simplifies filtering/aggregation ## Footnote Ensuring 1NF is crucial for effective data management.
27
When does **2NF** apply?
Only when the primary key is composite ## Footnote 2NF addresses partial dependencies in composite keys.
28
What is the rule for **2NF**?
No partial dependencies — every non-key column must depend on the whole key ## Footnote This ensures that all attributes are fully functionally dependent on the primary key.
29
What is the rule for **3NF**?
No transitive dependencies — non-key columns must depend only on the primary key ## Footnote This eliminates redundancy and ensures data integrity.
30
Example of fixing a **3NF violation**?
Move customer name and address from Orders into a separate Customers table ## Footnote This separates data into distinct entities to maintain normalization.