SQL - Theory Flashcards
What does the SELECT statement do in SQL?
It retrieves data from one or more tables in a database.
How do you filter records in a SQL query?
By using the WHERE clause.
What is the purpose of the JOIN clause?
The JOIN clause combines rows from two or more tables based on a related column between them.
What is the difference between INNER JOIN and LEFT JOIN?
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 do you select distinct values in a column?
By using the SELECT DISTINCT statement.
What does the GROUP BY clause do?
The GROUP BY clause groups rows that have the same values in specified columns into summary rows.
How do you count the number of rows in a table?
Using the COUNT(*) function.
How can you calculate the total sum of a column?
By using the SUM(column_name) function.
What is the purpose of the HAVING clause?
The purpose of the HAVING clause is to filter groups created by the GROUP BY clause based on a condition.
How do you order the results of a query?
By using the ORDER BY clause followed by the column name and sort direction (ASC or DESC).
What is a subquery?
A query nested inside another SQL query.
How do you create an index on a table?
Using the CREATE INDEX statement, e.g., CREATE INDEX idx_name ON table_name(column_name);
What is the purpose of an index in a database?
The purpose of an index is to improve the speed of data retrieval operations on a table.
Explain the difference between clustered and non-clustered indexes.
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.
What is a primary key?
A unique identifier for each record in a table that cannot contain NULL values.
How do you perform a case-insensitive search in SQL?
By using functions like LOWER() or UPPER() on both the column and the search string, e.g., WHERE LOWER(column_name) = ‘value’.
What is normalization?
Normalization is the process of organizing a database to reduce redundancy and improve data integrity.
What are aggregate functions? Name three examples.
Functions that perform a calculation on a set of values and return a single value. Examples: COUNT(), SUM(), AVG().