SQL - Theory Flashcards
(47 cards)
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().
What does the EXPLAIN statement do?
The EXPLAIN statement provides the execution plan for a SQL query, helping to analyze and optimize performance.
How can you remove duplicate rows from a result set?
By using the SELECT DISTINCT statement.
What is a foreign key?
A foreign key is a field in one table that uniquely identifies a row of another table, establishing a relationship between the two tables.
How do you update data in a table?
UPDATE statement with the SET clause, e.g., UPDATE table_name SET column1 = value1 WHERE condition;
How do you delete records from a table?
Using the DELETE FROM statement with a WHERE clause, e.g., DELETE FROM table_name WHERE condition;
What is the difference between WHERE and HAVING clauses?
WHERE filters rows before grouping, while HAVING filters groups after GROUP BY.