SQL - Theory Flashcards

1
Q

What does the SELECT statement do in SQL?

A

It retrieves data from one or more tables in a database.

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

How do you filter records in a SQL query?

A

By using the WHERE clause.

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

What is the purpose of the JOIN clause?

A

The JOIN clause combines rows from two or more tables based on a related column between them.

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

What is the difference between INNER JOIN and LEFT JOIN?

A

INNER JOIN returns only matching records from both tables, while LEFT JOIN returns 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
5
Q

How do you select distinct values in a column?

A

By using the SELECT DISTINCT statement.

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

What does the GROUP BY clause do?

A

The GROUP BY clause groups rows that have the same values in specified columns into summary rows.

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

How do you count the number of rows in a table?

A

Using the COUNT(*) function.

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

How can you calculate the total sum of a column?

A

By using the SUM(column_name) function.

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

What is the purpose of the HAVING clause?

A

The purpose of the HAVING clause is to filter groups created by the GROUP BY clause based on a condition.

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

How do you order the results of a query?

A

By using the ORDER BY clause followed by the column name and sort direction (ASC or DESC).

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

What is a subquery?

A

A query nested inside another SQL query.

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

How do you create an index on a table?

A

Using the CREATE INDEX statement, e.g., CREATE INDEX idx_name ON table_name(column_name);

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

What is the purpose of an index in a database?

A

The purpose of an index is to improve the speed of data retrieval operations on a table.

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

Explain the difference between clustered and non-clustered indexes.

A

A clustered index determines the physical order of data in a table, while a non-clustered index creates a separate structure pointing to the data.

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

What is a primary key?

A

A unique identifier for each record in a table that cannot contain NULL values.

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

How do you perform a case-insensitive search in SQL?

A

By using functions like LOWER() or UPPER() on both the column and the search string, e.g., WHERE LOWER(column_name) = ‘value’.

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

What is normalization?

A

Normalization is the process of organizing a database to reduce redundancy and improve data integrity.

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

What are aggregate functions? Name three examples.

A

Functions that perform a calculation on a set of values and return a single value. Examples: COUNT(), SUM(), AVG().

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

What does the EXPLAIN statement do?

A

The EXPLAIN statement provides the execution plan for a SQL query, helping to analyze and optimize performance.

20
Q

How can you remove duplicate rows from a result set?

A

By using the SELECT DISTINCT statement.

21
Q

What is a foreign key?

A

A foreign key is a field in one table that uniquely identifies a row of another table, establishing a relationship between the two tables.

22
Q

How do you update data in a table?

A

UPDATE statement with the SET clause, e.g., UPDATE table_name SET column1 = value1 WHERE condition;

23
Q

How do you delete records from a table?

A

Using the DELETE FROM statement with a WHERE clause, e.g., DELETE FROM table_name WHERE condition;

24
Q

What is the difference between WHERE and HAVING clauses?

A

WHERE filters rows before grouping, while HAVING filters groups after GROUP BY.

25
Q

How do you find the average value of a column?

A

Using the AVG(column_name) function.

26
Q

What is a view in SQL?

A

A virtual table based on the result-set of a SQL statement.

27
Q

How do you create a view?

A

Using the CREATE VIEW statement, e.g., CREATE VIEW view_name AS SELECT column1, column2 FROM table_name WHERE condition;

28
Q

What is a stored procedure?

A

A prepared SQL code that you can save and reuse, allowing for modular and efficient code execution.

29
Q

How do you create an index on multiple columns?

A

By listing multiple columns in the CREATE INDEX statement, e.g., CREATE INDEX idx_name ON table_name(column1, column2);

30
Q

What is a composite key?

A

A primary key composed of two or more columns to uniquely identify a record.

31
Q

Explain the UNION operator.

A

It combines the result sets of two or more SELECT statements, removing duplicate rows.

32
Q

How does UNION ALL differ from UNION?

A

UNION ALL includes all duplicate rows, while UNION removes duplicates.

33
Q

What is a transaction in SQL?

A

A sequence of one or more SQL operations treated as a single unit, ensuring data integrity.

34
Q

How do you start a transaction?

A

Using the BEGIN TRANSACTION or START TRANSACTION statement.

35
Q

What is the purpose of the COMMIT statement?

A

To save all changes made during the current transaction.

36
Q

How do you revert changes in a transaction?

A

Using the ROLLBACK statement.

37
Q

What is a window function?

A

A function that performs calculations across a set of table rows related to the current row, without collapsing the result set.

38
Q

Give an example of a window function.

A

ROW_NUMBER() OVER (PARTITION BY column ORDER BY column)

39
Q

How can you improve query performance?

A

By optimizing indexes, avoiding unnecessary columns in SELECT, using efficient joins, and analyzing execution plans.

40
Q

What does the DISTINCT keyword do?

A

It removes duplicate rows from the result set.

41
Q

How do you concatenate two columns in SQL?

A

Using the CONCAT() function or the || operator, e.g., SELECT CONCAT(first_name, ‘ ‘, last_name) AS full_name FROM table_name;

42
Q

What is the purpose of the LIKE operator?

A

To search for a specified pattern in a column.

43
Q

How do you perform a pattern match where a column starts with ‘A’?

A

WHERE column_name LIKE ‘A%’

44
Q

What is a CTE (Common Table Expression)?

A

A temporary result set defined within the execution scope of a single SELECT, INSERT, UPDATE, or DELETE statement.

45
Q

What is the NULL value in SQL?

A

It represents the absence of a value or an unknown value.

46
Q

How do you check for NULL values in a column?

A

Using the IS NULL or IS NOT NULL operators, e.g., WHERE column_name IS NULL.

47
Q

What is the purpose of the CASE statement?

A

To provide conditional logic in SQL queries, allowing different outputs based on conditions.