Top 20+ SQL Interview Questions & Answers

1. Q: What is SQL?

A: SQL (Structured Query Language) is a programming language used to manage and manipulate relational databases.

2. Q: What are the different types of SQL statements?

A: The different types of SQL statements are:

  • SELECT: Retrieves data from a database.
  • INSERT: Inserts new data into a database.
  • UPDATE: Modifies existing data in a database.
  • DELETE: Deletes data from a database.
  • CREATE: Creates a new database, table, or other database objects.
  • ALTER: Modifies the structure of a database object.
  • DROP: Deletes a database object.

3. Q: What is the syntax for the SELECT statement?

A: The syntax for the SELECT statement is as

follows: SELECT column1, column2 FROM table_name WHERE condition;

4. Q: How do you retrieve all records from a table?

A: To retrieve all records from a table, you can use the following SELECT

statement: SELECT * FROM table_name;

5. Q: How do you filter records based on a specific condition in the WHERE clause?

A: To filter records based on a specific condition, you can use the following SELECT

statement: SELECT column1, column2 FROM table_name WHERE condition;

6. Q: How do you sort records in ascending or descending order?

A: To sort records in ascending order, you can use the ORDER BY keyword followed by the column name. For descending order, use ORDER BY column_name DESC.

Example: SELECT column1, column2 FROM table_name ORDER BY column1 ASC;

7. Q: How do you insert new records into a table?

A: To insert new records into a table, you can use the INSERT INTO statement.

Example: INSERT INTO table_name (column1, column2) VALUES (value1, value2);

8. Q: How do you update existing records in a table?

A: To update existing records in a table, you can use the UPDATE statement.

Example: UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;

9. Q: How do you delete records from a table?

A: To delete records from a table, you can use the DELETE statement.

Example: DELETE FROM table_name WHERE condition;

10. Q: How do you join two or more tables together?

A: To join tables, you can use the JOIN keyword followed by the table names and the join condition.

Example: SELECT column1, column2 FROM table1 JOIN table2 ON table1.column = table2.column;

11. Q: What is the difference between INNER JOIN and OUTER JOIN?

A: An INNER JOIN returns only the matching records from both tables, while an OUTER JOIN returns all records from one table and the matching records from the other table.

12. Q: How do you count the number of records in a table?

A: To count the number of records in a table, you can use the COUNT() function.

Example: SELECT COUNT(*) FROM table_name;

13. Q: How do you group records based on a specific column?

A: To group records based on a specific column, you can use the GROUP BY clause.

Example: SELECT column1, COUNT(column2) FROM table_name GROUP BY column1;

14. Q: How do you find the maximum or minimum value in a column?

A: To find the maximum value, you can use the MAX() function. To find the minimum value, use the MIN() function.

Example: SELECT MAX(column1) FROM table_name;

SELECT MIN(column2) FROM table_name;

15. Q: How do you calculate the average or sum of a column?

A: To calculate the average, use the AVG() function. To calculate the sum, use the SUM() function.

Example: SELECT AVG(column1) FROM table_name;

SELECT SUM(column2) FROM table_name;

16. Q: How do you add a new column to an existing table?

A: To add a new column to an existing table, you can use the ALTER TABLE statement.

Example: ALTER TABLE table_name ADD column_name datatype;

17. Q: How do you modify the data type of a column in a table?

A: To modify the data type of a column in a table, you can use the ALTER TABLE statement with the MODIFY clause.

Example: ALTER TABLE table_name MODIFY column_name new_datatype;

18. Q: How do you delete a table from a database?

A: To delete a table from a database, you can use the DROP TABLE statement.

Example: DROP TABLE table_name;

19. Q: How do you create a new database?

A: To create a new database, you can use the CREATE DATABASE statement.

Example: CREATE DATABASE database_name;

20. Q: How do you select distinct values from a column?

A: To select distinct values from a column, you can use the DISTINCT keyword.

Example: SELECT DISTINCT column_name FROM table_name;