





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Table of Contents
2. Databases & Tables
SELECT COUNT(*) AS total, AVG(marks) AS average, MAX(marks) AS highest FROM students;
8. GROUP BY & HAVING
SELECT city, COUNT(*) AS total_students FROM students GROUP BY city;
SELECT department_id, AVG(marks) AS avg_marks FROM students GROUP BY department_id HAVING AVG(marks) >= 70;
9. INSERT, UPDATE & DELETE
INSERT INTO students (student_id,name,marks,city) VALUES (101,'Asha',82,'Nagpur');
UPDATE students SET marks= WHERE student_id=101;
DELETE FROM students WHERE student_id=101;
10. NULL Values
SELECT * FROM students WHERE city IS NULL; SELECT * FROM students WHERE city IS NOT NULL;
11. LIKE, IN & BETWEEN
SELECT name FROM students WHERE name LIKE 'A%'; SELECT * FROM students WHERE city IN ('Pune','Nagpur'); SELECT * FROM students WHERE marks BETWEEN 60 AND 80;
12. JOINs
SELECT s.name, d.department_name FROM students s INNER JOIN departments d ON s.department_id = d.department_id;
SELECT name, marks FROM students WHERE marks > (SELECT AVG(marks) FROM students);
14. CASE Expressions
SELECT name, marks, CASE WHEN marks >= 75 THEN 'Distinction' WHEN marks >= 60 THEN 'First Class' WHEN marks >= 40 THEN 'Pass' ELSE 'Fail' END AS result FROM students;
15. String Functions - UPPER() – uppercase. - LOWER() – lowercase. - TRIM() – remove surrounding spaces. - CONCAT() – combine text. - LENGTH()/LEN() – length depending on database.
SELECT UPPER(name) AS student_name FROM students;
16. Date & Numeric Functions
SELECT ROUND(price,2) FROM products; SELECT EXTRACT(YEAR FROM order_date) FROM orders;
17. Keys & Constraints - PRIMARY KEY – uniquely identifies a row. - FOREIGN KEY – links related tables. - NOT NULL – requires a value. - UNIQUE – prevents duplicates. - CHECK – validates a condition where supported. - DEFAULT – supplies a default value.
What is a JOIN?
What is normalization?
What is an index?
What is a view?
What is NULL?
Does correlation prove causation?
22. Practical SQL Queries
Find students above 80 marks.
SELECT * FROM students WHERE marks > 80;
Find students from Pune.
SELECT name FROM students WHERE city='Pune';
Find the highest marks.
SELECT MAX(marks) FROM students;
Count students by city.
SELECT city, COUNT(*) FROM students GROUP BY city;
Find average marks by department.
SELECT department_id, AVG(marks) FROM students GROUP BY department_id;
Find students above average.
SELECT name, marks FROM students WHERE marks > (SELECT AVG(marks) FROM students);
Find names beginning with A.
SELECT name FROM students WHERE name LIKE 'A%';
Find missing city values.
SELECT name FROM students WHERE city IS NULL;
Find top 5 marks.
SELECT name, marks FROM students ORDER BY marks DESC LIMIT 5;
Join students and departments.
SELECT s.name,d.department_name FROM students s JOIN departments d ON s.department_id=d.department_id;
23. Mini Database Project