1 Flashcards
What is the difference between INNER JOIN
and LEFT JOIN
?
INNER JOIN
returns only matching rows from both tables, while LEFT JOIN
returns all rows from the left table and matching rows from the right table, with NULLs where there are no matches.
What is a GROUP BY
clause used for?
GROUP BY
is used to group rows that have the same values in specified columns into summary rows, like COUNT, SUM, AVG, etc.
How does the HAVING
clause differ from WHERE
?
WHERE
filters rows before grouping, while HAVING
filters rows after grouping.
What is a Subquery
in SQL?
A subquery is a query nested inside another query, often used to return a single value or a set of values to be used in the main query.
What is a Common Table Expression
(CTE)?
A CTE is a temporary result set defined within the execution scope of a SELECT
, INSERT
, UPDATE
, or DELETE
statement.
What are window functions
in SQL? Provide examples.
Window functions perform calculations across a set of table rows related to the current row. Examples: ROW_NUMBER()
, RANK()
, LEAD()
, LAG()
.
What is a UNION
vs. UNION ALL
?
UNION
combines results from multiple queries and removes duplicates, while UNION ALL
combines all results, including duplicates.
What is the difference between COUNT(*)
and COUNT(column_name)
?
COUNT(*)
counts all rows, including NULLs, while COUNT(column_name)
counts only non-NULL values in the specified column.
Explain JOIN
types: INNER JOIN
, LEFT JOIN
, RIGHT JOIN
, FULL OUTER JOIN
.
-
INNER JOIN
: Returns matching rows from both tables. *LEFT JOIN
: Returns all rows from the left table and matched rows from the right. *RIGHT JOIN
: Returns all rows from the right table and matched rows from the left. *FULL OUTER JOIN
: Returns rows when there is a match in either left or right table.
How do you find duplicate records in SQL?
Use GROUP BY
and HAVING COUNT(*) > 1
to identify duplicate records.
What is a primary key
and a foreign key
?
A primary key
uniquely identifies each record in a table, while a foreign key
is a field in one table that links to the primary key of another table.
How do you perform a self join
in SQL?
A self join
is when a table is joined with itself. You must use table aliases to distinguish the two instances of the table.
What is a cross join
? Provide an example.
A cross join
returns the Cartesian product of two tables, combining every row from the first table with every row from the second table.
Explain the difference between CHAR
and VARCHAR
data types.
CHAR
stores fixed-length strings, while VARCHAR
stores variable-length strings.
What is Normalization
in database design?
Normalization is the process of organizing a database to reduce redundancy and dependency by dividing tables and ensuring relationships between them.
What is Denormalization
in database design?
Denormalization is the process of combining tables or adding redundant data to optimize query performance.
Explain Indexing
in SQL and how it improves query performance.
Indexing improves the speed of data retrieval operations by creating a structure that allows quicker searches, typically used on columns frequently involved in WHERE
, JOIN
, or ORDER BY
clauses.
What is a transaction
in SQL and how is it used?
A transaction is a set of SQL commands that are executed as a single unit, ensuring atomicity. Transactions are controlled with BEGIN
, COMMIT
, and ROLLBACK
.
What is the DISTINCT
keyword used for in SQL?
The DISTINCT
keyword is used to return only unique values from a column or set of columns.
What is GROUP_CONCAT()
function in MySQL or STRING_AGG()
in SQL Server?
These functions are used to concatenate values from multiple rows into a single string.
What is the difference between mean and median?
The mean is the average of all values, while the median is the middle value in a sorted data set.
What is standard deviation, and how is it calculated?
Standard deviation measures the amount of variation or dispersion in a set of values. It’s the square root of the variance.
Explain variance and its relation to standard deviation.
Variance is the average squared deviation from the mean, and standard deviation is the square root of variance.
What is a normal distribution?
A normal distribution is a bell-shaped probability distribution where most of the values cluster around the mean.