SQL Interview Questions and Answers
1. Basic SQL Queries (Beginner)
1. What is SQL?
SQL (Structured Query Language) is a standard programming language used to manage and
manipulate relational databases. It allows users to create, read, update, and delete data.
2. What are the different types of SQL commands?
- DDL (Data Definition Language): CREATE, ALTER, DROP
- DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE
- DCL (Data Control Language): GRANT, REVOKE
- TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT
3. Explain the purpose of SELECT, FROM, WHERE clauses.
- SELECT: Specifies the columns to retrieve.
- FROM: Specifies the table to query.
- WHERE: Filters records based on a condition.
4. How do you retrieve all columns and rows from a table?
SELECT * FROM table_name;
5. How do you filter rows based on a specific condition?
SELECT * FROM table_name WHERE column = 'value';
6. Explain the use of AND and OR in the WHERE clause.
- AND: All conditions must be true.
- OR: At least one condition must be true.
7. How do you sort the result set?
SELECT * FROM table_name ORDER BY column ASC/DESC;
8. How do you retrieve distinct values?
SELECT DISTINCT column FROM table_name;
9. What are aggregate functions?
COUNT(), SUM(), AVG(), MIN(), MAX()
10. How do you group rows and apply an aggregate function?
SELECT column, COUNT(*) FROM table_name GROUP BY column;
11. What is the purpose of HAVING?
HAVING filters groups after aggregation, whereas WHERE filters rows before grouping.
12. How do you limit rows returned?
MySQL/PostgreSQL: LIMIT 10
SQL Server: TOP 10
Oracle: WHERE ROWNUM <= 10
13. What are aliases?
Temporary names for columns/tables.
SELECT column AS alias FROM table_name;
... [Due to space, only part shown here; will include full content in PDF]