Did you know? Research improves critical thinking skills.

Important SQL Interview Questions with Examples - Sure Questions

SQL Interview Questions with Examples and Explanation

1. How to select all records from a table?

This query is used to fetch all data from a table.

SELECT * FROM students;
SELECT → means get data
* → means all columns
FROM students → means from students table
This query returns all rows and all columns from students table.

2. How to select specific columns?

This query fetches only required columns.

SELECT name, marks FROM students;
name, marks → only these two columns are selected
students → table name
Result will show only name and marks, not all columns.

3. How to use WHERE condition?

WHERE filters rows based on condition.

SELECT * FROM students WHERE marks > 60;
marks > 60 → condition
Only students who scored more than 60 will be displayed.

4. Difference between AND and OR?

SELECT * FROM students WHERE class='10' AND marks > 70;
class='10' → student must be in class 10
marks > 70 → student must score above 70
AND → both conditions must be true.

5. How to sort records?

SELECT * FROM students ORDER BY marks DESC;
ORDER BY → used for sorting
marks → column used for sorting
DESC → descending order (highest to lowest marks)

6. How to find unique values?

SELECT DISTINCT city FROM students;
DISTINCT → removes duplicate values
city → column name
Only unique city names will be displayed.

7. What is COUNT()?

SELECT COUNT(*) FROM students;
COUNT(*) → counts number of rows
Returns total number of students in table.

8. What is GROUP BY?

SELECT class, COUNT(*) FROM students GROUP BY class;
GROUP BY class → groups students by class
COUNT(*) → counts students in each class
Result: class-wise student count.

9. Difference between WHERE and HAVING?

SELECT class, COUNT(*) FROM students GROUP BY class HAVING COUNT(*) > 5;
GROUP BY class → group students by class
HAVING COUNT(*) > 5 → show only classes with more than 5 students
HAVING works after grouping.

10. What is JOIN?

SELECT students.name, orders.amount FROM students INNER JOIN orders ON students.id = orders.sid;
students.id = orders.sid → matching column
INNER JOIN → returns only matching rows
Output shows student name with their order amount.

11. What is Subquery?

SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
Inner query → finds average salary
Outer query → finds employees earning more than average salary.

12. Difference between DELETE and TRUNCATE?

DELETE FROM students WHERE id=5; TRUNCATE TABLE students;
DELETE → removes specific row (id=5)
TRUNCATE → removes all rows from table completely.

13. What is BETWEEN?

SELECT * FROM students WHERE marks BETWEEN 50 AND 80;
BETWEEN 50 AND 80 → range condition
Shows students with marks between 50 and 80.

14. What is LIKE?

SELECT * FROM students WHERE name LIKE 'A%';
LIKE → pattern search
A% → names starting with letter A.

15. INSERT example

INSERT INTO students VALUES(1,'Ravi',85,'Delhi');
Adds new row into students table with given values.

16. UPDATE example

UPDATE students SET marks=90 WHERE id=1;
SET marks=90 → new value
WHERE id=1 → update only that student.

17. PRIMARY KEY example

CREATE TABLE users( id INT PRIMARY KEY, name VARCHAR(50) );
PRIMARY KEY → unique and not null
Used to identify each record uniquely.

18. FOREIGN KEY example

CREATE TABLE orders( oid INT, uid INT, FOREIGN KEY(uid) REFERENCES users(id) );
FOREIGN KEY → links orders table with users table
uid refers to users.id.


Source: sureshtechlabs.com


Share this post:

WhatsApp Facebook Twitter Telegram