SQL Interview Questions and Answers
1. Basic SQL Queries (Beginner)
Q: What is SQL?
A: SQL (Structured Query Language) is a programming language designed for managing data in relational databases.
Q: What are the different types of SQL commands?
A: 1. DDL (Data Definition Language): CREATE, ALTER, DROP
2. DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE
3. DCL (Data Control Language): GRANT, REVOKE
4. TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT
Q: Explain the purpose of SELECT, FROM, WHERE clauses.
A: SELECT specifies the columns, FROM specifies the table, and WHERE filters rows based on conditions.
Q: How do you retrieve all columns and rows from a table?
A: SELECT * FROM table_name;
Q: How do you filter rows based on a specific condition?
A: Use the WHERE clause: SELECT * FROM table_name WHERE column_name = 'value';
Q: Explain the use of AND and OR operators in the WHERE clause.
A: AND requires all conditions to be true. OR requires at least one condition to be true.
Q: How do you sort the result set?
A: Use ORDER BY clause: SELECT * FROM table_name ORDER BY column_name ASC|DESC;
SQL Interview Questions and Answers
Q: How do you retrieve distinct values from a column?
A: Use DISTINCT: SELECT DISTINCT column_name FROM table_name;
Q: What are aggregate functions in SQL?
A: Functions that perform a calculation on a set of values: COUNT, SUM, AVG, MIN, MAX.
Q: How do you group rows based on a column and apply an aggregate function?
A: Use GROUP BY: SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name;
Q: What is the purpose of the HAVING clause?
A: Used to filter groups after aggregation. Different from WHERE which filters rows.
Q: How do you limit the number of rows returned by a query?
A: Use LIMIT (MySQL, PostgreSQL), TOP (SQL Server), ROWNUM (Oracle).
Q: What are aliases in SQL?
A: Aliases rename columns or tables using AS: SELECT column_name AS alias FROM table_name;
2. Intermediate SQL Queries
Q: What are joins in SQL?
A: Joins combine rows from two or more tables based on a related column.
Q: Explain the different types of joins.
A: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN.
SQL Interview Questions and Answers
Q: When would you use a specific type of join?
A: Use INNER JOIN when both tables must have matching records, LEFT JOIN to include all from the left table, etc.
Q: What is a self-join?
A: A self-join is a join of a table with itself.
Q: Explain the difference between UNION and UNION ALL.
A: UNION removes duplicates, UNION ALL includes duplicates.
Q: What are subqueries?
A: Queries within queries. Used to filter or calculate intermediate results.
Q: Explain correlated and non-correlated subqueries.
A: Correlated subqueries refer to outer queries; non-correlated do not.
Q: How do you check if a value exists in a set?
A: Use IN or NOT IN: SELECT * FROM table WHERE column IN ('A', 'B');
Q: How do you check if a value falls within a range?
A: Use BETWEEN: SELECT * FROM table WHERE column BETWEEN 10 AND 20;
Q: How do you perform pattern matching in SQL?
A: Use LIKE with wildcards: % for multiple characters, _ for single.
SQL Interview Questions and Answers
Q: What are NULL values?
A: Unknown or missing values. Use IS NULL or IS NOT NULL to filter.
Q: What are set operations in SQL?
A: UNION, INTERSECT, EXCEPT (or MINUS) combine result sets.
Q: How do you insert, update, and delete data?
A: INSERT INTO table VALUES (...), UPDATE table SET column=... WHERE ..., DELETE FROM table WHERE ...