SQL Interview Questions and Answers
Understanding basic SQL is essential to most roles in the tech industry, even for program and product managers!
What is a primary key?
A primary key is a column (or a combination of columns) in a database table that uniquely identifies each row. It is the unique identifier for each row. Some examples could be an order number, employee ID, car VIN, or account number.
What is a foreign key?
A foreign key is a column in one table that refers to the primary key of another table. It establishes a relationship between the two tables.
What is the order of operations of a SQL query?
SELECT column1
, COUNT(*) AS count
FROM table1
WHERE condition1
GROUP BY column1
HAVING COUNT(*) > 10
ORDER BY count DESC
LIMIT 10;
What are the different types of joins?
Inner join
Left join (or left outer)
Right join (or right outer)
Full outer join
Self join
What is a subquery?
Also known as a nested query or inner query, a subquery is a query embedded within another query.
How do you sort query results?
Order By
ASC (Ascending)
DESC (Descending)
How do you handle missing values in SQL?
IS NULL/IS NOT NULL
COALESCE()
What is a window function?
Window functions in allow you to perform calculations across a set of rows related to the current row. Unlike aggregate functions that collapse multiple rows into a single result, window functions preserve the row-level granularity of the data. Examples include ROW_NUMBER(), RANK(), DENSE_RANK(), LEAD(), LAG(), SUM(), and AVG().