SQL Query Interview Questions and Answers
Ques 31. Explain the purpose of the SQL CASE statement.
The CASE statement is used to perform conditional logic within a SQL query, similar to an IF-THEN-ELSE statement in other programming languages.
Example:
SELECT column1, CASE WHEN condition THEN 'Result1' ELSE 'Result2' END AS NewColumn FROM table;
Ques 32. What is a subquery? Provide an example.
A subquery is a query embedded within another query. It can be used to retrieve data that will be used in the main query. Example: SELECT column FROM table WHERE column IN (SELECT column FROM another_table);
Ques 33. Explain the difference between CHAR and VARCHAR data types.
CHAR is a fixed-length string data type, while VARCHAR is a variable-length string data type. CHAR pads spaces to the maximum length, while VARCHAR only stores the actual data without padding.
Ques 34. Write a SQL query to find the total count of rows in each table of a database.
SELECT table_name, COUNT(*) FROM information_schema.tables GROUP BY table_name;
Ques 35. Explain the purpose of the SQL HAVING clause.
The HAVING clause is used to filter the results of aggregate functions in a SELECT statement based on specified conditions.
Example:
SELECT column, COUNT(*) FROM table GROUP BY column HAVING COUNT(*) > 1;
Most helpful rated by users:
- What is SQL?
- What is the difference between SQL and MySQL?
- What is a primary key?
- What is an index in a database?
- What is a foreign key?