SQL Flashcards

(22 cards)

1
Q

What is an index in SQL?

A

An index is a database object that improves query performance by allowing faster lookups of rows, avoiding full table scans.

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

What is a clustered index?

A

A clustered index defines the physical order of rows in the table. Only one clustered index can exist per table.

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

What is the cost of a clustered index?

A

Updating a clustered index can be expensive because the table data itself must be reordered.

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

What is a non-clustered index?

A

A separate structure that stores indexed column values and pointers (row locators) to the actual table data.

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

Does a non-clustered index keep a copy of the data?

A

No, it keeps the indexed column plus a pointer to the actual row.

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

If you create a non-clustered index on salary in Employees (1000 rows, 100 distinct salaries), what does it store?

A

The index will store all salary values (including duplicates), each linked to row pointers, not just distinct values.

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

What is a full table scan?

A

Scanning all rows in a table to execute a query, usually happening when no suitable index exists.

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

What is an inner join?

A

Returns rows that have matching values in both tables.

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

What is a left join?

A

Returns all rows from the left table, plus matching rows from the right table (or NULL if no match).

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

What is a full outer join?

A

Returns all rows from both tables, matching where possible, NULL where not.

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

What is a Cartesian product in SQL?

A

When two tables are joined without a condition (or with CROSS JOIN), producing every combination of rows.

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

How do you find the second highest salary in SQL using TOP?

A

SELECT TOP 1 Salary
FROM Employees
WHERE Salary NOT IN (
SELECT TOP 1 Salary FROM Employees ORDER BY Salary DESC
)
ORDER BY Salary DESC;

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

How do you find the second highest salary in SQL using MAX?

A

SELECT MAX(Salary)
FROM Employees
WHERE Salary < (SELECT MAX(Salary) FROM Employees);

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

What is a transaction in SQL?

A

A sequence of SQL operations executed as a single unit that ensures ACID (Atomicity, Consistency, Isolation, Durability).

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

What are isolation levels in SQL?

A

They define how transactions interact with each other. Levels:

  • Read Uncommitted → allows dirty reads.
  • Read Committed → prevents dirty reads (default in SQL Server).
  • Repeatable Read → prevents dirty and non-repeatable reads.
  • Serializable → prevents dirty, non-repeatable, and phantom reads.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to set isolation levels?

A

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;

17
Q

When a row is locked, can it still be read?

A

Under Read Committed, other transactions must wait until the lock is released.

Under Read Uncommitted, the row can still be read (dirty read).

18
Q

What is a stored procedure?

A

A precompiled set of SQL statements stored in the database, used to encapsulate logic and reuse code.

19
Q

Benefits of stored procedures?

A

Performance (precompiled), reusability, security, maintainability.

20
Q

Downsides of stored procedures?

A

Can become complex, harder to version-control, not portable across DBMS, business logic tightly coupled to DB.

21
Q

What is a trigger?

A

A special stored procedure that automatically executes in response to an event (INSERT, UPDATE, DELETE) on a table.

22
Q

When to use triggers?

A

For enforcing business rules, maintaining audit logs, or automatically updating related data.