![]() |
VOOZH | about |
In the field of data science, SQL knowledge is often tested through a range of interview questions designed to assess both fundamental and advanced skills. These questions cover various aspects of SQL, including basic queries, data manipulation, aggregation functions, subqueries, joins, and performance optimization. Understanding these topics not only helps in tackling interview questions but also in applying SQL to real-world data challenges.
This comprehensive list of SQL interview questions and answers aims to provide data science professionals with a solid foundation in SQL, ensuring they are well-prepared to handle a variety of scenarios encountered in both interviews and practical applications. Whether you are a novice looking to build your SQL skills or an experienced practitioner seeking to refresh your knowledge, this guide offers valuable insights and examples to enhance your SQL proficiency.
Table of Content
SQL (Structured Query Language) is a standard language used for managing and manipulating relational databases. It allows users to perform tasks such as querying data, updating records, and managing schemas.
SQL databases are relational and use structured query language for defining and manipulating data. They are suitable for complex queries and transactions. NoSQL databases are non-relational and are used for unstructured data, offering flexibility in data storage and scalability.
Common SQL data types include:
INT: IntegerVARCHAR(length): Variable-length stringDATE: DateFLOAT: Floating-point numberBOOLEAN: True or False valueemployees.Example: "SELECT * FROM employees;"
Use the WHERE clause to filter records. Example: "SELECT * FROM employees WHERE department = 'Sales';"
GROUP BY clause? Give an example.GROUP BY is used to group rows that have the same values into summary rows. Example: "SELECT department, COUNT(*) FROM employees GROUP BY department;"
INNER JOIN and LEFT JOIN.INNER JOIN returns only rows with matching values in both tables. LEFT JOIN returns all rows from the left table and matched rows from the right table; unmatched rows from the right table are filled with NULLs.
INNER JOIN Example: "SELECT employees.name, departments.department FROM employees INNER JOIN departments ON employees.dept_id = departments.dept_id;"LEFT JOIN Example: "SELECT employees.name, departments.department FROM employees LEFT JOIN departments ON employees.dept_id = departments.dept_id;"Use the ORDER BY clause. Example: "SELECT * FROM employees ORDER BY salary DESC;"
HAVING clause? How does it differ from WHERE?HAVING is used to filter records after GROUP BY has been applied, while WHERE is used to filter records before grouping. Example using HAVING: "SELECT department, AVG(salary) FROM employees GROUP BY department HAVING AVG(salary) > 50000;"
salaries.Example: "SELECT salary FROM salaries ORDER BY salary DESC LIMIT 5;"
Use the AVG() function. Example: "SELECT AVG(salary) FROM employees;"
COUNT(*) and COUNT(column_name)?COUNT(*) counts all rows, including those with NULLs. COUNT(column_name) counts only rows where the specified column is not NULL.
COUNT(*): "SELECT COUNT(*) FROM employees;"COUNT(column_name): "SELECT COUNT(salary) FROM employees;"Use MAX() and MIN() functions.
MAX(): "SELECT MAX(salary) FROM employees;"MIN(): "SELECT MIN(salary) FROM employees;"CASE statement works in SQL.The CASE statement allows for conditional logic in SQL queries. Example: "SELECT name, CASE WHEN salary > 50000 THEN 'High' ELSE 'Low' END AS salary_status FROM employees;"
Example: "SELECT department, COUNT(*) FROM employees GROUP BY department;"
Window functions perform calculations across a set of table rows related to the current row. They are used for running totals, rankings, etc. Example: "SELECT name, salary, RANK() OVER (ORDER BY salary DESC) AS rank FROM employees;"
RANK() function with an example.The RANK() function assigns a rank to each row within a partition of the result set, with gaps in rank values if there are ties. Example: "SELECT name, salary, RANK() OVER (ORDER BY salary DESC) AS rank FROM employees;"
Use date functions and operators to add or subtract time intervals. Example: "SELECT CURRENT_DATE - INTERVAL '30' DAY AS past_date;"
CROSS JOIN and when would you use it?CROSS JOIN returns the Cartesian product of two tables, meaning every row of the first table is combined with every row of the second table. It is rarely used but useful in some scenarios. Example: "SELECT * FROM employees CROSS JOIN departments;"
Example: "SELECT month, SUM(sales) OVER (ORDER BY month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total FROM sales_data;"
A subquery is a query within another query used to perform operations that depend on the results of the outer query. Example: "SELECT name FROM employees WHERE department_id IN (SELECT department_id FROM departments WHERE department_name = 'Sales');"
WHERE clause?Subqueries in the WHERE clause are used to filter results based on the results of another query. Example: "SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);"
A correlated subquery refers to a subquery that references columns from the outer query. It is evaluated once for each row processed by the outer query. Example: "SELECT name FROM employees e1 WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e1.department_id = e2.department_id);"
UNION and UNION ALL.UNION combines the results of two queries and removes duplicates. UNION ALL combines results without removing duplicates.
UNION Example: "SELECT name FROM employees UNION SELECT name FROM managers;"UNION ALL Example: "SELECT name FROM employees UNION ALL SELECT name FROM managers;"Example: "SELECT name, salary FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);"
You can join three tables using multiple JOIN operations. Example: "SELECT e.name, d.department_name, p.project_name FROM employees e INNER JOIN departments d ON e.department_id = d.department_id INNER JOIN projects p ON e.project_id = p.project_id;"
A self-join is a join where a table is joined with itself. It is useful for hierarchical data or comparing rows within the same table. Example: "SELECT e1.name AS Employee, e2.name AS Manager FROM employees e1 LEFT JOIN employees e2 ON e1.manager_id = e2.employee_id;"
Use the DISTINCT keyword to remove duplicate rows. Example: "SELECT DISTINCT department FROM employees;"
EXISTS clause?The EXISTS clause is used to check if a subquery returns any results. It returns TRUE if the subquery returns one or more rows. Example: "SELECT name FROM employees WHERE EXISTS (SELECT * FROM departments WHERE departments.department_id = employees.department_id);"
Example: "SELECT customer_id FROM orders GROUP BY customer_id HAVING COUNT(order_id) > 3;"
Use the INSERT INTO statement. Example: "INSERT INTO employees (name, department, salary) VALUES ('John Doe', 'Marketing', 60000);"
UPDATE statement?The UPDATE statement modifies existing records in a table. Example: "UPDATE employees SET salary = 70000 WHERE name = 'John Doe';"
Use the DELETE FROM statement. Example: "DELETE FROM employees WHERE name = 'John Doe';"
Transactions are sequences of operations performed as a single unit of work. They ensure that a series of operations either all succeed or all fail, maintaining database integrity.
COMMIT and ROLLBACK?COMMIT makes all changes made in the transaction permanent. ROLLBACK undoes all changes made in the transaction.
COMMIT: "COMMIT;"ROLLBACK: "ROLLBACK;"Use ROLLBACK to revert changes if an error occurs, ensuring the database remains consistent.
ACID property in the context of SQL?ACID stands for Atomicity, Consistency, Isolation, and Durability, which are properties that guarantee reliable transactions in a database system.
Use parameterized queries or prepared statements to avoid SQL injection by ensuring that user inputs are properly escaped and treated as data rather than executable code.
Example: "UPDATE employees SET salary = salary * 1.1 WHERE department = 'Sales';"
Use the INSERT INTO ... VALUES statement with multiple value sets or use the LOAD DATA command for large datasets.
"INSERT INTO employees (name, department, salary) VALUES ('Alice', 'HR', 55000), ('Bob', 'IT', 60000);"LOAD DATA (MySQL): "LOAD DATA INFILE 'file_path.csv' INTO TABLE employees FIELDS TERMINATED BY ',';"An index is a database object that improves the speed of data retrieval operations on a table. It is crucial for optimizing query performance, especially with large datasets.
Use the CREATE INDEX statement. Example: "CREATE INDEX idx_salary ON employees(salary);"
Common strategies include:
SELECT statementsEXISTS instead of IN for subqueriesA clustered index determines the physical order of data in the table and there can be only one per table. A non-clustered index creates a logical order of data but does not affect the physical storage, and multiple non-clustered indexes can exist per table.
A query execution plan is a detailed description of how the database engine executes a query. It helps identify performance bottlenecks and optimize queries by showing how data retrieval is performed.
Strategies include:
Normalization is the process of organizing data to reduce redundancy and improve data integrity. It is important to ensure data consistency and efficient data management.
Denormalization involves merging tables or introducing redundancy to improve read performance. For example, storing aggregated data in a summary table to speed up queries. Example: "CREATE TABLE sales_summary AS SELECT department, SUM(sales) FROM sales GROUP BY department;"
Over-normalization can lead to excessive joins, which may degrade performance. It can also make the database schema complex and harder to manage.
Use tools like EXPLAIN or EXPLAIN ANALYZE to review query execution plans and identify slow-running queries. Analyze indices, examine query patterns, and optimize based on findings.