Advanced SQL Questions with Answers
Q: Create a table Employee with constraints: eid as primary key, email as unique, salary as
NOT NULL.
A: CREATE TABLE Employee (
eid INT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(100) UNIQUE,
salary DECIMAL(10,2) NOT NULL
);
Q: Alter the table to add a department column with default value 'HR'.
A: ALTER TABLE Employee ADD department VARCHAR(20) DEFAULT 'HR';
Q: Drop the constraint that makes email unique.
A: -- Note: MySQL doesn't support dropping named constraints easily unless you name them.
ALTER TABLE Employee DROP INDEX email;
Q: Write a query to show all NOT NULL columns from a table.
A: SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Employee' AND IS_NULLABLE = 'NO';
Q: Display all student names in uppercase.
A: SELECT UPPER(name) FROM Student;
Q: Find the length of the longest student name.
A: SELECT MAX(CHAR_LENGTH(name)) AS longest_name_length FROM Student;
Q: Extract the domain name from email addresses (after '@').
A: SELECT SUBSTRING_INDEX(email, '@', -1) AS domain FROM Employee;
Q: Round the average marks to 2 decimal places.
A: SELECT ROUND(AVG(marks), 2) AS avg_marks FROM Student;
Q: Find students whose names contain the letter 'n'.
A: SELECT * FROM Student WHERE name LIKE '%n%';
Q: Find students whose name is 5 letters long.
A: SELECT * FROM Student WHERE name LIKE '_____';
Q: Find names that end with 'a' and start with 'R'.
A: SELECT * FROM Student WHERE name LIKE 'R%a';
Q: Select employees who joined in 2023.
A: SELECT * FROM Employee WHERE YEAR(join_date) = 2023;
Q: Count how many students are above 18 as of today's date.
A: SELECT COUNT(*) FROM Student WHERE age > 18;
Q: Calculate how many days are left until the exam (use CURDATE()).
A: SELECT DATEDIFF('2024-05-01', CURDATE()) AS days_left;
Q: List students who scored more than the average marks of age 20 group.
A: SELECT * FROM Student
WHERE marks > (
SELECT AVG(marks) FROM Student WHERE age = 20
);
Q: Find students who have the exact same marks as someone else.
A: SELECT * FROM Student s1
WHERE EXISTS (
SELECT * FROM Student s2
WHERE s1.id != s2.id AND s1.marks = s2.marks
);
Q: Get student(s) who have highest age among those who scored above 80.
A: SELECT * FROM Student
WHERE age = (
SELECT MAX(age) FROM Student WHERE marks > 80
);
Q: Find student with highest total marks across all subjects.
A: SELECT s.id, s.name, SUM(m.mark) AS total
FROM Student s
JOIN Marks m ON s.id = m.id
GROUP BY s.id, s.name
ORDER BY total DESC
LIMIT 1;
Q: Feedback table with CASE WHEN rating logic.
A: SELECT id,
CASE
WHEN rating > 4 THEN 'Good'
WHEN rating >= 3 THEN 'Average'
ELSE 'Poor'
END AS feedback
FROM Feedback;
Q: Count how many students passed and failed using GROUP BY.
A: SELECT
CASE WHEN marks >= 40 THEN 'Pass' ELSE 'Fail' END AS status,
COUNT(*) AS count
FROM Student
GROUP BY status;
Q: Spot the error: SELECT name, AVG(marks) FROM Student;
A: -- Error: GROUP BY missing
SELECT name, AVG(marks) FROM Student GROUP BY name;
Q: Why does DELETE FROM Student GROUP BY age not work?
A: -- DELETE does not support GROUP BY in standard SQL.
-- You must delete using a subquery or WHERE condition.
Q: Fix this: SELECT * FROM Student WHERE name = 'Aman' OR 'Riya';
A: SELECT * FROM Student WHERE name = 'Aman' OR name = 'Riya';