Sql Syntax & Basic Queries Interview Questions
Comprehensive sql syntax & basic queries interview questions and answers for SQL. Prepare for your next job interview with expert guidance.
Questions Overview
1. What is the basic structure of a SELECT statement in SQL?
Basic2. How do you select all columns from a table?
Basic3. Explain the purpose of the WHERE clause in SQL.
Basic4. What are the comparison operators used in SQL WHERE clauses?
Moderate5. How do you use the DISTINCT keyword in SQL?
Basic6. What is the ORDER BY clause and how is it used?
Moderate7. Explain how the LIMIT clause works in SQL.
Moderate8. What is the difference between AND and OR logical operators?
Basic9. How do you use the LIKE operator for pattern matching?
Moderate10. What is the purpose of the IN operator?
Basic11. Explain the BETWEEN operator in SQL.
Basic12. How do you handle NULL values in SQL?
Moderate13. What is the difference between CHAR and VARCHAR data types?
Moderate14. How do you use aliases in SQL queries?
Basic15. Explain the concept of subqueries in SQL.
Advanced16. What are the main SQL data types?
Basic17. How do you use the GROUP BY clause?
Moderate18. Explain the HAVING clause in SQL.
Advanced19. What are aggregate functions in SQL?
Moderate20. How do you use the CASE statement in SQL?
Advanced21. What is the difference between WHERE and HAVING clauses?
Moderate22. Explain string manipulation functions in SQL.
Advanced23. How do you handle date and time in SQL?
Moderate24. What are the different types of SQL comments?
Basic25. Explain the use of wildcard characters in SQL.
Moderate26. What is the difference between DELETE and TRUNCATE?
Moderate27. How do you combine results from multiple queries?
Advanced28. Explain the concept of query execution order.
Advanced29. What is the basic structure of a SELECT statement in SQL?
Basic30. How do you select all columns from a table?
Basic31. Explain the purpose of the WHERE clause in SQL.
Basic32. What are the comparison operators used in SQL WHERE clauses?
Moderate33. How do you use the DISTINCT keyword in SQL?
Basic34. What is the ORDER BY clause and how is it used?
Moderate35. Explain how the LIMIT clause works in SQL.
Moderate36. What is the difference between AND and OR logical operators?
Basic37. How do you use the LIKE operator for pattern matching?
Moderate38. What is the purpose of the IN operator?
Basic39. Explain the BETWEEN operator in SQL.
Basic40. How do you handle NULL values in SQL?
Moderate41. What is the difference between CHAR and VARCHAR data types?
Moderate42. How do you use aliases in SQL queries?
Basic43. Explain the concept of subqueries in SQL.
Advanced44. What are the main SQL data types?
Basic45. How do you use the GROUP BY clause?
Moderate46. Explain the HAVING clause in SQL.
Advanced47. What are aggregate functions in SQL?
Moderate48. How do you use the CASE statement in SQL?
Advanced49. What is the difference between WHERE and HAVING clauses?
Moderate50. Explain string manipulation functions in SQL.
Advanced51. How do you handle date and time in SQL?
Moderate52. What are the different types of SQL comments?
Basic53. Explain the use of wildcard characters in SQL.
Moderate54. What is the difference between DELETE and TRUNCATE?
Moderate55. How do you combine results from multiple queries?
Advanced56. Explain the concept of query execution order.
Advanced1. What is the basic structure of a SELECT statement in SQL?
BasicThe basic structure of a SELECT statement is SELECT columns FROM table [WHERE condition]. It allows you to retrieve data from one or more tables, with SELECT specifying which columns to retrieve, FROM indicating the table, and WHERE (optional) filtering the results.
2. How do you select all columns from a table?
BasicTo select all columns from a table, use the asterisk (*) wildcard in the SELECT statement. For example: SELECT * FROM table_name. This retrieves all columns and rows from the specified table.
3. Explain the purpose of the WHERE clause in SQL.
BasicThe WHERE clause is used to filter records based on specific conditions. It allows you to retrieve only the rows that meet the specified criteria, reducing the amount of data returned and helping to pinpoint exact information.
4. What are the comparison operators used in SQL WHERE clauses?
ModerateSQL comparison operators include: = (equal), <> or != (not equal), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to), BETWEEN (range), LIKE (pattern matching), IN (multiple possible values), and IS NULL (null value check).
5. How do you use the DISTINCT keyword in SQL?
BasicThe DISTINCT keyword is used to remove duplicate rows from the result set. For example: SELECT DISTINCT column_name FROM table_name. It returns only unique values in the specified column(s).
6. What is the ORDER BY clause and how is it used?
ModerateThe ORDER BY clause is used to sort the result set in ascending (ASC) or descending (DESC) order. For example: SELECT * FROM table_name ORDER BY column_name DESC. By default, sorting is in ascending order if not specified.
7. Explain how the LIMIT clause works in SQL.
ModerateThe LIMIT clause restricts the number of rows returned in a query result. For example: SELECT * FROM table_name LIMIT 10 returns only the first 10 rows. Some databases use TOP or FETCH FIRST instead of LIMIT.
8. What is the difference between AND and OR logical operators?
BasicAND requires all conditions to be true, while OR requires at least one condition to be true. For example, WHERE age > 30 AND salary < 50000 returns rows meeting both conditions, while WHERE age > 30 OR salary < 50000 returns rows meeting either condition.
9. How do you use the LIKE operator for pattern matching?
ModerateThe LIKE operator is used for pattern matching with wildcard characters. % represents zero or more characters, _ represents a single character. For example: WHERE name LIKE 'A%' finds names starting with A, WHERE name LIKE '_oh%' finds names with second and third letters 'oh'.
10. What is the purpose of the IN operator?
BasicThe IN operator allows you to specify multiple values in a WHERE clause. It provides a shorthand for multiple OR conditions. For example: WHERE column_name IN (value1, value2, value3) is equivalent to WHERE column_name = value1 OR column_name = value2 OR column_name = value3.
11. Explain the BETWEEN operator in SQL.
BasicThe BETWEEN operator selects values within a given range. It is inclusive of both boundary values. For example: WHERE age BETWEEN 20 AND 30 returns rows where age is 20, 30, or any value in between.
12. How do you handle NULL values in SQL?
ModerateNULL values are handled using IS NULL and IS NOT NULL operators. For example: WHERE column_name IS NULL finds rows with null values, while WHERE column_name IS NOT NULL finds rows with non-null values. Standard comparison operators do not work with NULL.
13. What is the difference between CHAR and VARCHAR data types?
ModerateCHAR is a fixed-length string type that pads shorter strings with spaces, while VARCHAR is a variable-length string type that only uses the space needed. CHAR(10) always uses 10 characters, but VARCHAR(10) can use 1 to 10 characters.
14. How do you use aliases in SQL queries?
BasicAliases provide alternative names for tables or columns in a query. Column aliases use AS keyword: SELECT first_name AS name. Table aliases are used for readability and in joins: FROM employees AS e.
15. Explain the concept of subqueries in SQL.
AdvancedA subquery is a query nested inside another query. It can be used in SELECT, FROM, WHERE, and HAVING clauses. For example: SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees) returns employees with above-average salary.
16. What are the main SQL data types?
BasicCommon SQL data types include: INTEGER/INT (whole numbers), DECIMAL/NUMERIC (precise decimal numbers), FLOAT/REAL (approximate numeric), CHAR/VARCHAR (fixed/variable strings), DATE/DATETIME (date and time values), BOOLEAN (true/false), and BLOB (binary large objects).
17. How do you use the GROUP BY clause?
ModerateThe GROUP BY clause groups rows with the same values in specified columns into summary rows. It is typically used with aggregate functions like COUNT, SUM, AVG. For example: SELECT department, AVG(salary) FROM employees GROUP BY department calculates average salary per department.
18. Explain the HAVING clause in SQL.
AdvancedThe HAVING clause filters groups created by GROUP BY, similar to WHERE but applied after grouping. For example: SELECT department, AVG(salary) FROM employees GROUP BY department HAVING AVG(salary) > 50000 shows departments with average salary above 50000.
19. What are aggregate functions in SQL?
ModerateAggregate functions perform calculations on a set of values and return a single result. Common aggregate functions include COUNT() (number of rows), SUM() (total), AVG() (average), MAX() (maximum value), and MIN() (minimum value).
20. How do you use the CASE statement in SQL?
AdvancedThe CASE statement allows conditional logic in queries. It works like an IF-THEN-ELSE statement. Example: SELECT name, CASE WHEN salary > 50000 THEN 'High' ELSE 'Low' END AS salary_category FROM employees.
21. What is the difference between WHERE and HAVING clauses?
ModerateWHERE filters individual rows before grouping, while HAVING filters groups after grouping. WHERE works with individual row conditions, HAVING works with aggregate function conditions in grouped queries.
22. Explain string manipulation functions in SQL.
AdvancedCommon string functions include CONCAT() (combine strings), SUBSTRING() (extract part of string), LENGTH() (string length), UPPER() (uppercase), LOWER() (lowercase), TRIM() (remove spaces), and REPLACE() (replace substring).
23. How do you handle date and time in SQL?
ModerateSQL provides functions for date and time manipulation like CURRENT_DATE, DATE_ADD(), DATE_SUB(), DATEDIFF() to perform calculations and comparisons. Specific functions vary between database systems.
24. What are the different types of SQL comments?
BasicSQL supports two types of comments: single-line comments (-- comment text) and multi-line comments (/* comment text */). Comments are used to explain code and are ignored by the database engine.
25. Explain the use of wildcard characters in SQL.
ModerateWildcard characters are used with LIKE operator: % matches zero or more characters, _ matches a single character, [] matches any single character in brackets, [^] matches any character not in brackets. They enable flexible pattern matching in queries.
26. What is the difference between DELETE and TRUNCATE?
ModerateDELETE removes rows one at a time and can be rolled back, TRUNCATE removes all rows at once and cannot be rolled back. DELETE can use a WHERE clause, TRUNCATE removes all data from a table.
27. How do you combine results from multiple queries?
AdvancedSet operators like UNION (removes duplicates), UNION ALL (includes duplicates), INTERSECT (common rows), and EXCEPT (rows in first query not in second) combine results from multiple SELECT statements.
28. Explain the concept of query execution order.
AdvancedThe typical SQL query execution order is: FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT. This means conditions are applied before grouping, aggregations, sorting, and limiting results.
29. What is the basic structure of a SELECT statement in SQL?
BasicThe basic structure of a SELECT statement is SELECT columns FROM table [WHERE condition]. It allows you to retrieve data from one or more tables, with SELECT specifying which columns to retrieve, FROM indicating the table, and WHERE (optional) filtering the results.
30. How do you select all columns from a table?
BasicTo select all columns from a table, use the asterisk (*) wildcard in the SELECT statement. For example: SELECT * FROM table_name. This retrieves all columns and rows from the specified table.
31. Explain the purpose of the WHERE clause in SQL.
BasicThe WHERE clause is used to filter records based on specific conditions. It allows you to retrieve only the rows that meet the specified criteria, reducing the amount of data returned and helping to pinpoint exact information.
32. What are the comparison operators used in SQL WHERE clauses?
ModerateSQL comparison operators include: = (equal), <> or != (not equal), > (greater than), < (less than), >= (greater than or equal to), <= (less than or equal to), BETWEEN (range), LIKE (pattern matching), IN (multiple possible values), and IS NULL (null value check).
33. How do you use the DISTINCT keyword in SQL?
BasicThe DISTINCT keyword is used to remove duplicate rows from the result set. For example: SELECT DISTINCT column_name FROM table_name. It returns only unique values in the specified column(s).
34. What is the ORDER BY clause and how is it used?
ModerateThe ORDER BY clause is used to sort the result set in ascending (ASC) or descending (DESC) order. For example: SELECT * FROM table_name ORDER BY column_name DESC. By default, sorting is in ascending order if not specified.
35. Explain how the LIMIT clause works in SQL.
ModerateThe LIMIT clause restricts the number of rows returned in a query result. For example: SELECT * FROM table_name LIMIT 10 returns only the first 10 rows. Some databases use TOP or FETCH FIRST instead of LIMIT.
36. What is the difference between AND and OR logical operators?
BasicAND requires all conditions to be true, while OR requires at least one condition to be true. For example, WHERE age > 30 AND salary < 50000 returns rows meeting both conditions, while WHERE age > 30 OR salary < 50000 returns rows meeting either condition.
37. How do you use the LIKE operator for pattern matching?
ModerateThe LIKE operator is used for pattern matching with wildcard characters. % represents zero or more characters, _ represents a single character. For example: WHERE name LIKE 'A%' finds names starting with A, WHERE name LIKE '_oh%' finds names with second and third letters 'oh'.
38. What is the purpose of the IN operator?
BasicThe IN operator allows you to specify multiple values in a WHERE clause. It provides a shorthand for multiple OR conditions. For example: WHERE column_name IN (value1, value2, value3) is equivalent to WHERE column_name = value1 OR column_name = value2 OR column_name = value3.
39. Explain the BETWEEN operator in SQL.
BasicThe BETWEEN operator selects values within a given range. It is inclusive of both boundary values. For example: WHERE age BETWEEN 20 AND 30 returns rows where age is 20, 30, or any value in between.
40. How do you handle NULL values in SQL?
ModerateNULL values are handled using IS NULL and IS NOT NULL operators. For example: WHERE column_name IS NULL finds rows with null values, while WHERE column_name IS NOT NULL finds rows with non-null values. Standard comparison operators do not work with NULL.
41. What is the difference between CHAR and VARCHAR data types?
ModerateCHAR is a fixed-length string type that pads shorter strings with spaces, while VARCHAR is a variable-length string type that only uses the space needed. CHAR(10) always uses 10 characters, but VARCHAR(10) can use 1 to 10 characters.
42. How do you use aliases in SQL queries?
BasicAliases provide alternative names for tables or columns in a query. Column aliases use AS keyword: SELECT first_name AS name. Table aliases are used for readability and in joins: FROM employees AS e.
43. Explain the concept of subqueries in SQL.
AdvancedA subquery is a query nested inside another query. It can be used in SELECT, FROM, WHERE, and HAVING clauses. For example: SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees) returns employees with above-average salary.
44. What are the main SQL data types?
BasicCommon SQL data types include: INTEGER/INT (whole numbers), DECIMAL/NUMERIC (precise decimal numbers), FLOAT/REAL (approximate numeric), CHAR/VARCHAR (fixed/variable strings), DATE/DATETIME (date and time values), BOOLEAN (true/false), and BLOB (binary large objects).
45. How do you use the GROUP BY clause?
ModerateThe GROUP BY clause groups rows with the same values in specified columns into summary rows. It is typically used with aggregate functions like COUNT, SUM, AVG. For example: SELECT department, AVG(salary) FROM employees GROUP BY department calculates average salary per department.
46. Explain the HAVING clause in SQL.
AdvancedThe HAVING clause filters groups created by GROUP BY, similar to WHERE but applied after grouping. For example: SELECT department, AVG(salary) FROM employees GROUP BY department HAVING AVG(salary) > 50000 shows departments with average salary above 50000.
47. What are aggregate functions in SQL?
ModerateAggregate functions perform calculations on a set of values and return a single result. Common aggregate functions include COUNT() (number of rows), SUM() (total), AVG() (average), MAX() (maximum value), and MIN() (minimum value).
48. How do you use the CASE statement in SQL?
AdvancedThe CASE statement allows conditional logic in queries. It works like an IF-THEN-ELSE statement. Example: SELECT name, CASE WHEN salary > 50000 THEN 'High' ELSE 'Low' END AS salary_category FROM employees.
49. What is the difference between WHERE and HAVING clauses?
ModerateWHERE filters individual rows before grouping, while HAVING filters groups after grouping. WHERE works with individual row conditions, HAVING works with aggregate function conditions in grouped queries.
50. Explain string manipulation functions in SQL.
AdvancedCommon string functions include CONCAT() (combine strings), SUBSTRING() (extract part of string), LENGTH() (string length), UPPER() (uppercase), LOWER() (lowercase), TRIM() (remove spaces), and REPLACE() (replace substring).
51. How do you handle date and time in SQL?
ModerateSQL provides functions for date and time manipulation like CURRENT_DATE, DATE_ADD(), DATE_SUB(), DATEDIFF() to perform calculations and comparisons. Specific functions vary between database systems.
52. What are the different types of SQL comments?
BasicSQL supports two types of comments: single-line comments (-- comment text) and multi-line comments (/* comment text */). Comments are used to explain code and are ignored by the database engine.
53. Explain the use of wildcard characters in SQL.
ModerateWildcard characters are used with LIKE operator: % matches zero or more characters, _ matches a single character, [] matches any single character in brackets, [^] matches any character not in brackets. They enable flexible pattern matching in queries.
54. What is the difference between DELETE and TRUNCATE?
ModerateDELETE removes rows one at a time and can be rolled back, TRUNCATE removes all rows at once and cannot be rolled back. DELETE can use a WHERE clause, TRUNCATE removes all data from a table.
55. How do you combine results from multiple queries?
AdvancedSet operators like UNION (removes duplicates), UNION ALL (includes duplicates), INTERSECT (common rows), and EXCEPT (rows in first query not in second) combine results from multiple SELECT statements.
56. Explain the concept of query execution order.
AdvancedThe typical SQL query execution order is: FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, LIMIT. This means conditions are applied before grouping, aggregations, sorting, and limiting results.