SQL Intermediate Flashcards

(40 cards)

1
Q

What is a subquery in SQL?

A

A query nested inside another SQL query

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

How do you use a subquery in a WHERE clause?

A

SELECT * FROM employees WHERE department_id = (SELECT id FROM departments WHERE name = ‘Sales’);

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

What is a correlated subquery?

A

A subquery that uses values from the outer query and runs once per row

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

What is a common table expression (CTE)?

A

A temporary named result set defined using WITH

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

How do you define a CTE?

A

WITH temp AS (SELECT * FROM employees) SELECT * FROM temp;

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

What is the difference between a CTE and a subquery?

A

CTEs improve readability and can be reused; subqueries are embedded and more limited

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

What is a window function?

A

A function that performs calculations across a set of table rows related to the current row

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

What does ROW_NUMBER() do?

A

Assigns a unique sequential number to each row in the result set

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

What is PARTITION BY in window functions?

A

Divides rows into groups before applying the function

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

What does RANK() do?

A

Assigns a rank to rows within a partition

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

How do you find the second highest salary using RANK?

A

SELECT * FROM (SELECT salary

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

What is the purpose of COALESCE()?

A

Returns the first non-NULL value in a list

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

What does NVL() do in SQL?

A

Replaces NULL with a specified value (Oracle-specific; COALESCE is standard)

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

What is a self-join?

A

A table joined to itself

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

How do you perform a self-join?

A

Use table aliases: SELECT a.name

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

What does the IN operator do?

A

Checks if a value exists in a list or result of a subquery

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

What is the difference between IN and EXISTS?

A

IN compares values; EXISTS checks for row existence and can be faster for correlated subqueries

18
Q

How do you enforce uniqueness in a column?

A

Use a UNIQUE constraint

19
Q

What is a composite primary key?

A

A primary key made of more than one column

20
Q

What are indexes in SQL?

A

Data structures that improve the speed of queries on a database table

21
Q

How do you create an index on the name column?

A

CREATE INDEX idx_name ON employees(name);

22
Q

What are the downsides of indexes?

A

They speed up reads but slow down writes and increase storage

23
Q

What is normalization?

A

The process of organizing data to reduce redundancy and improve integrity

24
Q

What is the goal of first normal form (1NF)?

A

Ensure each column contains atomic values and each row is unique

25
What is the goal of second normal form (2NF)?
Remove partial dependencies; every non-key attribute must depend on the whole key
26
What is the goal of third normal form (3NF)?
Remove transitive dependencies; non-key attributes must depend only on the key
27
What is denormalization?
The process of introducing redundancy to improve read performance
28
What is a transaction in SQL?
A sequence of operations performed as a single logical unit of work
29
What are the ACID properties?
Atomicity
30
What is the COMMIT statement?
Saves the changes made during the transaction permanently
31
What does the ROLLBACK statement do?
Reverts changes made during the transaction
32
What is the difference between TRUNCATE and DELETE?
TRUNCATE removes all rows quickly without logging; DELETE can be selective and is logged
33
What is a view in SQL?
A virtual table created by a SELECT query
34
How do you create a view?
CREATE VIEW view_name AS SELECT column1 FROM table_name;
35
What are materialized views?
Views that store the query result physically and can be refreshed
36
What is a UNION in SQL?
Combines results of two SELECTs and removes duplicates
37
What is UNION ALL?
Combines results of two SELECTs and includes duplicates
38
How do you change a column’s data type?
ALTER TABLE table_name ALTER COLUMN column_name TYPE new_data_type;
39
What is a constraint in SQL?
A rule enforced on data to ensure integrity (e.g.
40
How do you add a NOT NULL constraint to a column?
ALTER TABLE table_name MODIFY column_name datatype NOT NULL;