Question Bank - Basic [SQL] Consider a example table as -
step 1 - shoot following query to create a tablestep
*** CREATE TABLE student (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), age
INT, email VARCHAR(100), gender VARCHAR(10), grade CHAR(1),marks INT, city
VARCHAR(50),registration_date DATE);
2 - Shoot following query to insert data in table
*** INSERT INTO student (name, age, email, gender, grade, marks, city,
registration_date) VALUES
('Alice', 20, '[email protected]', 'Female', 'A', 88,
'Delhi', '2024-06-01'),
('Bob', 21, '[email protected]', 'Male', 'B', 75, 'Mumbai',
'2024-05-21'),
('Carol', 22, '[email protected]', 'Female', 'C', 64,
'Chennai', '2024-03-14'),
('David', 19, '[email protected]', 'Male', 'A', 92, 'Pune',
'2024-04-10'),
('Eva', 23, '[email protected]', 'Female', 'B', 78, 'Delhi',
'2024-06-10'),
('Frank', 20, '[email protected]', 'Male', 'C', 55,
'Bangalore', '2024-01-29'),
('Grace', 18, '[email protected]', 'Female', 'A', 95,
'Mumbai', '2024-05-05'),
('Harry', 21, '[email protected]', 'Male', 'B', 70,
'Kolkata', '2024-06-01'),
('Ivy', 20, '[email protected]', 'Female', 'C', 60,
'Hyderabad', '2024-02-15'),
('Jack', 22, '[email protected]', 'Male', 'A', 85, 'Delhi',
'2024-03-20'),
('Kathy', 24, '[email protected]', 'Female', 'B', 77,
'Chennai', '2024-04-25'),
('Liam', 19, '[email protected]', 'Male', 'C', 58, 'Pune',
'2024-01-01'),
('Mona', 21, '[email protected]', 'Female', 'A', 90,
'Delhi', '2024-05-30'),
('Nate', 20, '[email protected]', 'Male', 'B', 74,
'Bangalore', '2024-03-15'),
('Olivia', 22, '[email protected]', 'Female', 'C', 69,
'Kolkata', '2024-04-12'),
('Paul', 23, '[email protected]', 'Male', 'A', 83, 'Mumbai',
'2024-06-18'),
('Quinn', 18, '[email protected]', 'Female', 'B', 72,
'Hyderabad', '2024-05-22'),
('Rita', 20, NULL, 'Female', 'C', 65, 'Chennai', '2024-03-
01'),
('Steve', 21, '[email protected]', 'Male', 'A', 88,
'Delhi', '2024-06-24'),
('Tina', 19, '[email protected]', 'Female', 'B', 76, 'Pune',
'2024-04-02');
✅ Basic SQL Questions (Single Table)
🔹 SELECT & FILTERING (Q1–Q15)
1. Select all columns from the student table.
****Answer/SQL Query === select * from student;
2. Select only names and ages of students.
****Answer/SQL Query === select name , age from student;
3. Find students who are older than 18.
****Answer/SQL Query === select * from student where age >18;
4. Find students who are exactly 20 years old.
****Answer/SQL Query === select * from Student where age =20;
5. Find students with marks greater than or equal to 75.
****Answer/SQL Query === SELECT * FROM student WHERE marks >= 75;
6. Find students living in 'Delhi'.
****Answer/SQL Query === select * from student where city='delhi';
7. Find female students.
****Answer/SQL Query === select * from student where gender='female';
8. Find students whose name starts with 'A'.
****Answer/SQL Query === select * from student where name like 'A%';
9. Find students whose name ends with 'n'.
****Answer/SQL Query === select * from student where name like '%n';
10.Find students whose name contains 'an'.
****Answer/SQL Query === select * from student where name like '%an%';
11.Find students whose marks are between 60 and 80.
****Answer/SQL Query === select * from student where marks between 60 and 80;
12.Find students whose city is either 'Delhi', 'Mumbai', or 'Chennai'.
****Answer/SQL Query === select * from student where city IN
('delhi','mumbai','chennai');
13.Find students who are not from 'Pune'.
****Answer/SQL Query === select * from student where city != 'pune'; OR select *
from
student where city not in ('pune');
14.Find students who are not female.
****Answer/SQL Query === select * from student where gender != 'female';
15.Find students with null emails.
****Answer/SQL Query === select * from student where email is null;
🔹ORDER BY, LIMIT (Q16–Q20)
16.List all students sorted by marks in descending order.
****Answer/SQL Query === select * from student order by marks desc;
17.List top 5 students with highest marks.
****Answer/SQL Query === select * from student order by marks desc limit 5;
18.List bottom 3 students with lowest marks.
****Answer/SQL Query === select * from student order by marks asc limit 3;
19.List students ordered by name alphabetically.
****Answer/SQL Query === select * from student order by name desc;
20.List students registered most recently (latest date first).
****Answer/SQL Query === select * from student order by registration_date desc;
🔹AGGREGATE FUNCTIONS (Q21–Q30)
21.Count total number of students. ****Answer/SQL Query
=== select count(*) from student;
22.Find the average age of students. ****Answer/SQL Query
=== select avg(age) from student;
23.Find the maximum marks. ****Answer/SQL Query
=== select max(marks) from student;
24.Find the minimum age. ****Answer/SQL Query
=== select min(age) from student;
25.Find the total marks of all students. ****Answer/SQL Query
=== select sum(marks) from student;
26.Count how many students are from 'Delhi'. ****Answer/SQL Query
=== select count(*) from student where city = 'delhi';
27.Count male and female students. ****Answer/SQL Query
=== select gender,count(*) from student GROUP BY gender;
28.Find average marks for each grade. ****Answer/SQL Query
=== select grade,avg(marks) AS average_marks from student group by grade
29.Count number of students in each city. **Answer/SQL Query===
select city,count(*) AS Number_OF_Student_each_City from student group by city;
30.Find max, min, and avg marks. ****Answer/SQL Query
=== select max(marks),avg(marks),min(marks) from student;
🔹GROUP BY & HAVING (Q31–Q35)
31.Group students by age. ****Answer/SQL
Query=== select age,count(*) from student group by age;
32.Find cities with more than 3 students. ****Answer/SQL
Query=== select city,count(*) from student group by city having count(*) >3;
33.Find grades with average marks above 80. ****Answer/SQL
Query=== select grade,avg(marks) from student group by grade having avg(marks) >
80;
34.List all genders with at least 2 students. ****Answer/SQL
Query=== select gender,count(*) from student group by gender having count(*) >= 2;
35.Find ages with more than 1 student. ****Answer/SQL
Query=== select age,count(*) from student group by age having count(*) > 1;
🔹MODIFY TABLE STRUCTURE (Q36–Q40)
36.Add a new column phone to the student table.
****Answer/SQL Query === alter table student add Phone bigint;
37.Change the grade column from CHAR(1) to VARCHAR(2).
****Answer/SQL Query === alter table student modify grade VARCHAR(2);
38.Rename the marks column to score.
****Answer/SQL Query === alter table student change marks Score int;
39.Drop the column email.
****Answer/SQL Query === alter table student drop column email;
40.Set default value for city as 'Unknown'.
****Answer/SQL Query === alter table student alter column city set default
'Unknown';
🔹UPDATE & DELETE (Q41–Q45)
41.Update marks to 100 for student named 'John'.
****Answer/SQL Query === update student set score = 100 where name = 'bob';
42.Increase all student marks by 5.
****Answer/SQL Query === update student set score = score +5;
43.Set city to 'Mumbai' for all students with null ci
****Answer/SQL Query === update student set city = 'Mumbai' where city is null;
44.Delete all students with marks less than 35.
****Answer/SQL Query === DELETE FROM student WHERE score < 35;
45.Delete students from 'Chennai'.
****Answer/SQL Query === delete from student where city = 'Chennai';
🔹MISCELLANEOUS (Q46–Q50)
46.Get current date in SQL.
****Answer/SQL Query === select curdate() as today;
47.Find students registered in year 2024.
****Answer/SQL Query === SELECT * FROM student WHERE YEAR(registration_date) =
2024;
48.Find length of each student’s name.
****Answer/SQL Query === SELECT name, CHAR_LENGTH(name) FROM student;
49.Convert student names to uppercase.
****Answer/SQL Query === SELECT name, UPPER(name) FROM student;
50.Display names with ‘Mr.’ prefix (use CONCAT).
****Answer/SQL Query === SELECT name, CONCAT('Mr. ', name) FROM student;
Document by: MR__SAWANT__46