Top SQL Interview Questions and Answers (TechTFQ) Flashcards
The bulk of the questions in this deck were sourced from content provided by Thoufiq Mohammed from TechTFQ on YouTube and his blog. Youtube: https://www.youtube.com/watch?v=AK7_m-aThfw&list=PLavw5C92dz9E0Vc2WVkFRHW3Nmncu5caL&index=5 Blog: https://techtfq.com/blog/top-20-sql-interview-questions Some additional SQL interview questions have been added to these based on personal study.
Name the 5 types of SQL commands.
Question Level: Expect this question for beginner and intermediate level role.
- Data Definition Language (DDL)
* CREATE, DROP, ALTER, TRUNCATE, COMMENT, RENAME - Data Manipulation Language (DML)
* INSERT, UPDATE, DELETE, MERGE, LOCK - Data Control Language (DCL)
* GRANT, REVOKE - Transaction Control Language (TCL)
* COMMIT, ROLLBACK, SAVEPOINT, SET TRANSACTION - Data Query Language (DQL)
* SELECT
What are DDL commands? Give some examples.
Question Level: Expect this question for beginner and intermediate level role.
DDL stands for Data Definition Language. They include CREATE, DROP, ALTER and TRUNCATE statements.
DDL statements are used to create, remove or modify database objects like table. You do not need to commit the changes after running DDL commands.
CREATE statement can be used to create any database objects like tables, views, functions, procedures, triggers etc.
DROP statement can be used to remove any database objects like tables, views, functions, procedures, triggers etc.
ALTER statement can be used to modify the structure of a database objects.
TRUNCATE statement can be used to remove all the data from a table at once.
What are DML commands? Give some examples.
Question Level: Expect this question for beginner and intermediate level role.
DML stands for Data Manipulation Language. DML includes INSERT, UPDATE, DELETE and MERGE statements.
DML statements are used to add, remove or modify data from database tables. It is mandatory to run the COMMIT command after running a DML statement so as to save the changes to the database (some tools may have auto commit on so you don’t have to manually run the commit command).
INSERT statement will add rows or records to a table.
UPDATE statement will modify the data in the table.
DELETE statement will remove one or multiple rows from a table.
MERGE statement will either do an update or insert to a table based on the available data. If the data is present then it does an update. If data not present then merge will do an insert.
What are DCL commands? Give some examples.
Question Level: Expect this question for beginner and intermediate level role.
DCL stands for Data Control Language. DCL includes GRANT and REVOKE statements.
GRANT statements are used to provide access privileges to a database object to any database or schema.
REVOKE statements are used to remove access privileges from a database object from any database or schema.
What are TCL commands? Give some examples.
Question Level: Expect this question for beginner and intermediate level role.
TCL stands for Transaction Control Language. TCL includes COMMIT, ROLLBACK, SAVEPOINT and SET TRANSACTION.
COMMIT statement will permanently save any open transactions in the current session to the database. By transaction, I mean any changes done to any database table using any of the DML statements like INSERT, UPDATE, DELETE and MERGE.
ROLLBACK statement will remove (unsave) any open transactions in the current session to the database. So all un committed transactions in the current session will be lost.
SAVEPOINT statement can be used to create a specific pointer in your session and provide a name to this pointer. You can then either rollback or commit transactions only until this point (savepoint name) rather than committing or rollbacking all the transaction in the session.
SET TRANSACTION Specify characteristics for the transaction
What are DQL commands? Give some examples.
Question Level: Expect this question for beginner and intermediate level role.
DQL stands for Data Query Language. It includes only the SELECT statement.
SELECT statement is used to fetch and view data from the database.
What is the difference between DELETE and TRUNCATE statement?
Question Level: Expect this question for beginner and intermediate level role.
Delete can be used to remove either few or all the records from a table.
Whereas truncate will always remove all the records from the table. Truncate cannot have WHERE condition.
Delete is a DML statement hence we will need to commit the transaction in order to save the changes to database.
Whereas truncate is a DDL statement hence no commit is required.
For example:
Below statement will delete only the records from employee table where the name is ‘Thoufiq’
DELETE FROM employee WHERE name = ‘Thoufiq’;
COMMIT;
Below statement will delete all records from the employee table.
DELETE FROM employee;
COMMIT;
Below statement will also delete all the records from the employee table. No commit is required here.
TRUNCATE TABLE employee;
Why do we use CASE Statement in SQL? Give example
Question Level: Expect this question for beginner and intermediate level role.
CASE statement is similar to IF ELSE statement from any other programming languages. We can use it to fetch or show a particular value based on certain condition.
For example:
SELECT CASE WHEN gender = ‘M’ THEN ‘Male’
WHEN gender = ‘F’ THEN ‘female’
ELSE ‘Other’
END AS gender
FROM employee;
In above example query, we display the gender as ‘Male’ when the gender column in the employee table has value as ‘M’. And if the gender column has value as ‘F’ then we display the value as ‘Female’.
If the gender column has values anything other than M or F then the query would return ‘Other’ (which we have defined using the ELSE part in the CASE statement.
Name 6 types of SQL joins?
Question Level: Expect this question for beginner, intermediate and senior level role.
1) Inner Join (Join)
2) Left Join (Left Outer Join)
3) Right Join (Right Outer Join)
4) Full Join (Full Outer Join)
5) Self Join
6) Cross Join
What is an Inner Join?
Question Level: Expect this question for beginner, intermediate and senior level role.
An INNER JOIN will fetch only those records which are present in both the joined tables.
The matching of the records is only based on the columns used for joining these two tables. INNER JOIN can also be represented as JOIN in your SELECT query.
Example INNER JOIN Query:
SELECT
cr.country_name,
ct.continent_name
FROM continents AS ct
INNER JOIN countries AS cr
ON ct.continent_code = cr.continent_code;
What is a Left Join?
Question Level: Expect this question for beginner, intermediate and senior level role.
A LEFT JOIN will fetch all records from the left table (table 1 placed on the left side during the join), and the matching records from the right table (table2). The result is 0 records from the right side, if there is no match.
The LEFT JOIN keyword returns all records from the left table (Customers), even if there are no matches in the right table (Orders).
LEFT JOIN can also be represented as LEFT OUTER JOIN in your SELECT query.
Example LEFT JOIN Query:
SELECT
cr.country_name,
ct.continent_name
FROM continents AS ct
LEFT JOIN countries AS cr
ON ct.continent_code = cr.continent_code;
What is a Right Join?
Question Level: Expect this question for beginner, intermediate and senior level role.
A RIGHT JOIN (OR RIGHT OUTER JOIN) will fetch all records from the right table (table placed on the right side during the join), and the matching records from the left table (table1). The result is 0 records from the left side, if there is no match.
If your select clause has a column from the left table then for records which are not present in left table (but present in right table), SQL will return a NULL value.
The RIGHT JOIN keyword returns all records from the right table (Employees), even if there are no matches in the left table (Orders).
*Note: LEFT and RIGHT join depends on whether the table is placed on the left side of the JOIN or on the right side of the JOIN.
Example RIGHT JOIN Query:
SELECT
cr.country_name,
ct.continent_name
FROM continents AS ct
RIGHT JOIN countries AS cr
ON ct.continent_code = cr.continent_code;
What is an Full Join?
Question Level: Expect this question for beginner, intermediate and senior level role.
A FULL JOIN will fetch all records from both left and right table. It’s kind of combination of INNER, LEFT and RIGHT join.
Meaning FULL JOIN will fetch all the matching records in left and right table + all the records from left table (even if these records are not present in right table) + all the records from right table (even if these records are not present in left table).
FULL JOIN can also be represented as FULL OUTER JOIN in your SELECT query.
Example FULL OUTER JOIN Query:
SELECT
cr.country_name,
ct.continent_name
FROM continents AS ct
FULL OUTER JOIN countries AS cr
ON ct.continent_code = cr.continent_code;
What is an Self Join?
Question Level: Expect this question for beginner, intermediate and senior level role.
A SELF JOIN is when you join a table to itself.
There is no keyword like SELF when doing this join. We just use the normal INNER join to do a self join.
Just that instead of doing an inner join with two different table, we inner join the same table to itself. These tables should have different alias names.
Other than this, SELF join performs similar to INNER join.
Example SELF JOIN Query:
SELECT
cr1.country_name
FROM countries AS cr1
JOIN countries AS cr2
ON cr1.country_code = cr2.continent_code;
What is an Natural Join?
Question Level: Expect this question for beginner, intermediate and senior level role.
A NATURAL JOIN is similar to INNER join but we do not need to use the ON clause during the join. Meaning in a natural join we just specify the tables.
We do not specify the columns based on which this join should work. By default when we use NATURAL JOIN, SQL will join the two tables based on the common column name in these two tables.
So when doing the natural join, both the tables need to have columns with same name and these columns should have same data type.
Example NATURAL JOIN Query:
SELECT
cr.country_name,
ct.continent_name
FROM continents AS ct
NATURAL JOIN countries AS cr;
What is an Cross Join?
Question Level: Expect this question for beginner, intermediate and senior level role.
A CROSS JOIN will join all the records from left table with all the records from right table. Meaning the cross join is not based on matching any column.
Whether there is a match or not, cross join will return records which is basically number of records in left table multiplied by number of records in right table.
In other words, cross join returns a Cartesian product.
Example CROSS JOIN Query:
SELECT
cr.country_name,
ct.continent_name
FROM continents AS ct
CROSS JOIN countries AS cr;
What is the difference between DISTINCT and GROUP BY?
Question Level: Expect this question for beginner and intermediate level role.
DISTINCT clause will return unique column values.
Depending on the list of columns you provide the DISTINCT clause, it will fetch the unique combination of values for all those combined columns.
If you provide just a single column in DISTINCT then it fetches just the unique values in that specific column. Example below:
Below query returns unique employee names from the employee table:
SELECT DISTINCT name FROM employee;
Whereas below query would return unique combination of values based on all the columns from the employee table.
SELECT DISTINCT * FROM employee;
GROUP BY clause will group together the data based on the columns specified in group by. Which will then return just one record for each unique value in the column specified in group by.
In other words GROUP BY can also be used to fetch unique records from a table but this is not why group by clause is used for.
The main purpose of group by clause is to perform some aggregation (using the aggregate functions like MIN, MAX, COUNT, SUM, AVG) based on the grouped by column values. Example below:
Below query would group together the data from employee table based on name column and then for each name value, it would count how many records have the same name.
SELECT name, COUNT(1) FROM employee GROUP BY name;
What are the rules to follow when using UNION operator?
Question Level: Expect this question for beginner and intermediate level role.
UNION operator can be used to combine two different SQL Queries. The output would be the result combined from both these queries. Duplicate records would not be returned.
You can combine two queries using UNION operator if they follow the below rules:
* Both queries must return same same no of columns.
* The columns in both the queries must be in same order.
* Data type of all the columns in both the queries must be same.
What are aggregate functions? Name and explain different types of aggregate functions in SQL?
Question Level: Expect this question for beginner and intermediate level role.
Aggregate function can be used to perform calculation on a set of values, which will then return a single value. We can use aggregate function either with GROUP BY clause or without it.
SUM: Calculates the sum of given values.
AVG: Calculate the average from the given set of values.
MIN: Find the minimum value in the given set of values.
MAX: Find the maximum value in the given set of values.
COUNT: Returns the number of records or rows.
Explain the SUM Aggregate Function
SUM: Calculates the sum of given values.
Below query will find the sum of all the salaries from the entire employee table:
SELECT SUM(salary) as total_salary FROM employee;
Whereas the below query will return the sum of salaries for each department in the employee table:
SELECT dept_id, SUM(salary) as total_salary_per_dept
FROM employee
GROUP BY dept_id;