Preparing for an SQL Server interview requires a solid understanding of SQL basics, including essential commands like SELECT, INSERT, UPDATE, DELETE, and various clauses like WHERE, ORDER BY, and GROUP BY. Below are 30 SQL interview questions and answers, broken down by topics, to help you master the basics and impress your interviewers.
SELECT
statement is used to retrieve data from one or more tables in a database. You can specify columns and filter results using conditions.INSERT
statement is used to add new rows into a table. You can insert specific values into columns or use DEFAULT
to insert default values.UPDATE
statement is used to modify existing records in a table. It requires a SET
clause to define the new values and a WHERE
clause to filter the rows to update.DELETE
statement removes rows from a table based on a specified condition. Be cautious when using it, as without a WHERE
clause, it will delete all rows.DELETE
removes rows one by one, while TRUNCATE
removes all rows from a table quickly, but it cannot be rolled back (unless in a transaction).WHERE
clause is used to filter records based on specified conditions. It restricts the rows that are returned or affected by SQL statements like SELECT
, UPDATE
, and DELETE
.ORDER BY
clause is used to sort the results of a query in either ascending (ASC
) or descending (DESC
) order based on one or more columns.GROUP BY
clause is used to group rows that have the same values in specified columns, often used with aggregate functions like COUNT
, SUM
, AVG
, etc.HAVING
clause is used to filter groups created by the GROUP BY
clause. It allows filtering based on aggregate functions, which cannot be done with the WHERE
clause.WHERE
clause filters rows before grouping, while the HAVING
clause filters groups after they are formed by GROUP BY
. WHERE
cannot be used with aggregate functions directly.SUM
function is an aggregate function that returns the total sum of a numeric column. It is often used with GROUP BY
to calculate totals for each group.AVG
function calculates the average value of a numeric column, ignoring NULL
values, and is typically used in conjunction with the GROUP BY
clause.COUNT
function returns the number of rows that match a specified condition. You can use COUNT(*)
for total rows, or COUNT(column_name)
to count non-NULL
values.COUNT(*)
counts all rows in a table, including those with NULL
values, while COUNT(column_name)
counts only the rows where the specified column is not NULL
.AVG
function like this: SELECT AVG(salary) FROM employees;
This will return the average salary from the employees
table.DISTINCT
to perform calculations on unique values. For example, SELECT COUNT(DISTINCT column_name) FROM table_name;
counts distinct values.MAX
function returns the highest value from a column. It is commonly used to find the maximum number in a set of data, like the highest salary or most recent date.MIN
function returns the smallest value in a specified column. It can be used with numeric, date, and other comparable data types.GROUP BY
to calculate a single result for the entire table. For example, SELECT AVG(salary) FROM employees;
returns the average for the whole table.GROUP BY
with COUNT
: SELECT department_id, COUNT(*) FROM employees GROUP BY department_id;
.LIKE
operator is used to search for a specified pattern in a column. It supports wildcards such as %
(for any sequence of characters) and _
(for a single character).BETWEEN
operator to filter values within a specific range. For example: SELECT * FROM table_name WHERE column_name BETWEEN 10 AND 20;
.INNER JOIN
returns only the matching rows from both tables, while a LEFT JOIN
returns all rows from the left table and matching rows from the right table, with NULL
for non-matching rows.DISTINCT
keyword removes duplicate rows from the result set. It ensures that each row is unique based on the selected columns.NULL
value represents the absence of a value in a column. It is different from an empty string or zero and requires special handling in queries.UNION
or UNION ALL
operators to combine results from multiple SELECT
statements. UNION
removes duplicates, while UNION ALL
includes duplicates.IN
and EXISTS
operators in SQL?
IN
operator is used to check if a value exists in a list of values, while EXISTS
checks if a subquery returns any rows. IN
is typically used with lists, and EXISTS
with subqueries.UPDATE
statement with a WHERE
clause to specify which rows to update. For example: UPDATE table_name SET column_name = value WHERE condition;
.CASE
statement is used for conditional logic. It allows you to return different values based on specified conditions, similar to an IF-ELSE structure.