SQL Interview Questions and Answers
1. Q: What is SQL?
A: SQL stands for Structured Query Language, used to communicate with databases.
2. Q: What are the types of SQL commands?
A: They include DDL, DML, DCL, TCL, and DQL.
3. Q: What is a primary key?
A: A primary key uniquely identifies each row in a table and cannot be NULL.
4. Q: What is a foreign key?
A: A foreign key links two tables and refers to the primary key in another table.
5. Q: What is the difference between WHERE and HAVING?
A: WHERE filters rows before grouping, HAVING filters after grouping.
6. Q: What is the difference between INNER JOIN and LEFT JOIN?
A: INNER JOIN returns matching rows; LEFT JOIN returns all rows from the left table.
7. Q: What is normalization?
A: The process of organizing data to reduce redundancy and improve integrity.
8. Q: What is denormalization?
A: The process of combining tables to improve read performance.
9. Q: What is a constraint?
A: Rules applied on columns to limit the data, e.g., NOT NULL, UNIQUE.
10. Q: What is an index?
A: A database object that improves the speed of data retrieval.
11. Q: What is a view?
A: A virtual table based on the result of an SQL query.
12. Q: What is the difference between DELETE, TRUNCATE, and DROP?
A: DELETE removes specific rows; TRUNCATE removes all rows; DROP removes the table.
13. Q: What is a subquery?
A: A query within another SQL query.
14. Q: What is a stored procedure?
A: A group of SQL statements stored and executed in the database.
15. Q: What is a trigger?
A: A procedure that runs automatically in response to events on a table.
16. Q: What is the difference between UNION and UNION ALL?
A: UNION removes duplicates; UNION ALL includes duplicates.
17. Q: What is a NULL value?
A: A field with no value.
18. Q: How do you check for NULL values?
A: Using IS NULL or IS NOT NULL.
19. Q: What is a transaction?
A: A unit of work performed within a database.
20. Q: What are ACID properties?
A: Atomicity, Consistency, Isolation, Durability.
21. Q: What is a cursor?
A: A pointer used to retrieve rows from a result set one at a time.
22. Q: What is the use of GROUP BY?
A: To group rows with the same values in specified columns.
23. Q: What is the difference between CHAR and VARCHAR?
A: CHAR has fixed length; VARCHAR has variable length.
24. Q: What is a schema?
A: A collection of database objects like tables, views, and procedures.
25. Q: What is a composite key?
A: A primary key made of two or more columns.
26. Q: What is a unique key?
A: It ensures all values in a column are different.
27. Q: What is a self join?
A: A table joined with itself.
28. Q: What is a cross join?
A: Returns the Cartesian product of two tables.
29. Q: How do you find duplicate records in a table?
A: Using GROUP BY and HAVING COUNT > 1.
30. Q: What is an alias in SQL?
A: A temporary name for a table or column.
31. Q: What is CASE in SQL?
A: Used for conditional logic within queries.
32. Q: How do you sort data in SQL?
A: Using the ORDER BY clause.
33. Q: How do you get the current date in SQL?
A: Using functions like GETDATE() or CURRENT_DATE.
34. Q: What is the LIMIT clause?
A: Used to specify the number of records to return.
35. Q: What is a default constraint?
A: Assigns a default value when no value is specified.
36. Q: What are wildcards in SQL?
A: Symbols like %, _, used with LIKE for pattern matching.
37. Q: What is a correlated subquery?
A: A subquery that uses values from the outer query.
38. Q: What is EXISTS in SQL?
A: Checks if a subquery returns any records.
39. Q: How do you rename a table?
A: Using the RENAME or ALTER TABLE command.
40. Q: What is the difference between SQL and MySQL?
A: SQL is a language; MySQL is a database system.
41. Q: What is data integrity?
A: Ensuring accuracy and consistency of data.
42. Q: What is a temporary table?
A: A table created for short-term use, often within a session.
43. Q: What is the BETWEEN operator?
A: Used to filter values within a range.
44. Q: How do you fetch even or odd rows in SQL?
A: Using modulo: % 2 = 0 for even, % 2 != 0 for odd.
45. Q: What is COALESCE()?
A: Returns the first non-null value among its arguments.
46. Q: What is the IFNULL() function?
A: Replaces NULL with a specified value.
47. Q: What is the difference between IN and EXISTS?
A: IN compares with a list; EXISTS checks for result presence.
48. Q: What is the use of DISTINCT?
A: Removes duplicate values in results.
49. Q: How do you find the second highest salary in a table?
A: Using subqueries or LIMIT with OFFSET.
50. Q: How do you avoid SQL injection?
A: Using prepared statements or parameterized queries.
Coding Questions and Answers
1. Q: Write a program to reverse a string.
A: Use slicing like string[::-1] or a loop.
2. Q: Find the factorial of a number.
A: Use recursion or a loop multiplying from 1 to n.
3. Q: Check if a number is prime.
A: Divide n by all numbers from 2 to sqrt(n).
4. Q: Find the largest element in an array.
A: Use max() or iterate and track the max.
5. Q: Check if a string is a palindrome.
A: Compare the string with its reverse.
6. Q: Find the Fibonacci sequence up to n terms.
A: Use a loop and store previous two values.
7. Q: Check if a number is even or odd.
A: Use modulo operator: n % 2.
8. Q: Swap two numbers without using a third variable.
A: Use arithmetic: a = a + b, b = a - b, a = a - b.
9. Q: Find duplicates in a list.
A: Use a set or a dictionary to count occurrences.
10. Q: Count the number of vowels in a string.
A: Iterate and check if each char is a vowel.
11. Q: Sort an array without using built-in sort.
A: Use bubble sort, selection sort, etc.
12. Q: Find the sum of digits of a number.
A: Use a loop: sum += n % 10; n //= 10.
13. Q: Reverse a number.
A: Use a loop to extract and build reverse.
14. Q: Find the length of a string without using len().
A: Use a loop to count characters.
15. Q: Find the GCD of two numbers.
A: Use Euclidean algorithm.
16. Q: Find the LCM of two numbers.
A: Use formula: LCM = (a*b)//GCD.
17. Q: Check if two strings are anagrams.
A: Sort both and compare or use a frequency map.
18. Q: Find the missing number in an array 1 to n.
A: Sum of n natural numbers - sum of array.
19. Q: Check if a year is a leap year.
A: Divisible by 4, but not by 100 unless also by 400.
20. Q: Implement binary search.
A: Use divide and conquer by halving the search space.
21. Q: Find the first non-repeating character in a string.
A: Use a dictionary to count and check.
22. Q: Check if a string contains only digits.
A: Use str.isdigit() or iterate and check each char.
23. Q: Convert a decimal number to binary.
A: Use bin() or repeated division by 2.
24. Q: Implement a stack using a list.
A: Use append() for push and pop() for pop.
25. Q: Find the second largest number in an array.
A: Track max and second max.
26. Q: Reverse a linked list.
A: Use iterative method with 3 pointers.
27. Q: Check if a linked list has a cycle.
A: Use Floyd's cycle detection algorithm.
28. Q: Implement a queue using two stacks.
A: Use two stacks to simulate enqueue/dequeue.
29. Q: Find intersection of two arrays.
A: Use sets or loops to find common elements.
30. Q: Implement a basic calculator.
A: Parse the expression and evaluate with stack for precedence.