Queries¶
Insert queries¶
-
Insert a row with all columns
INSERT INTO students VALUES (1, 'Tantia', 'Tope', 't@t.com', '1234567890', 1);
-
Insert a row with some columns
INSERT INTO students (first_name, last_name) VALUES ('Tantia', 'Tope');
Select Queries¶
-
Get all students
SELECT * FROM students;
-
Get first and last name of all students
SELECT first_name, last_name FROM students;
-
Get first name of all students with output column name as
Student Name
4. Get all unique addresses of all studentsSELECT first_name AS "Student Name" FROM students;
5. Get all students with ID equal to 1SELECT DISTINCT address FROM students;
6. Get all students with IQ greater than 150SELECT * FROM students WHERE id = 1;
7. Get all students with IQ less than 100SELECT * FROM students WHERE iq > 150;
8. Get all students with IQ greater than 100 and less than150SELECT * FROM students WHERE iq < 100;
9. Get all students with IQ greater than 100 or less than 150SELECT * FROM students WHERE iq > 100 AND iq < 150;
10. Get all students with first nameSELECT * FROM students WHERE iq BETWEEN 100 AND 150;
Tantia
11. Get all students with first nameSELECT * FROM students WHERE first_name = 'Tantia';
Tantia
and last nameTope
12. Get all students with first nameSELECT * FROM students WHERE first_name = 'Tantia' AND last_name = 'Tope';
John
or first nameMycroft
13. Get all students with nameSELECT * FROM students WHERE first_name = 'John' OR first_name = 'Mycroft';
John Watson
orMycroft Holmes
14. Get all students without the first nameSELECT * FROM students WHERE (first_name = 'John' AND last_name = 'Watson') OR (first_name = 'Mycroft' AND last_name = 'Holmes');
John
15. Get all students without the first nameSELECT * FROM students WHERE first_name <> 'John';
John
or last nameMycroft
16. Get all students with first name starting withSELECT * FROM students WHERE first_name <> 'John' AND last_name <> 'Mycroft';
T
17. Get all students with last name ending withSELECT * FROM students WHERE first_name LIKE 'T%';
walker
18. Get all students with first name containingSELECT * FROM students WHERE last_name LIKE '%walker';
T
19. Get all students with last name in the formatSELECT * FROM students WHERE first_name LIKE '%T%';
___walker
20. Get all students in Jhansi and LondonSELECT * FROM students WHERE last_name LIKE '___walker';
21. Get all students which do not have a batch idSELECT * FROM students WHERE address IN ('Jhansi', 'London');
22. Get the first 5 studentsSELECT * FROM students WHERE batch_id IS NULL;
23. Get the first 5 students sorted by IQSELECT * FROM students LIMIT 5;
24. Get the first 5 students sorted by IQ in descending orderSELECT * FROM students ORDER BY iq LIMIT 5;
25. Get the first 5 students sorted by IQ in descending order and then by first nameSELECT * FROM students ORDER BY iq DESC LIMIT 5;
SELECT * FROM students ORDER BY iq DESC, first_name LIMIT 5;
Update Queries¶
-
Update a row
2. Update a row with a conditionUPDATE students SET first_name = 'Tantia' WHERE id = 1;
UPDATE students SET first_name = 'Tantia' WHERE id = 1 AND first_name = 'John';
-
Update multiple columns
UPDATE students SET first_name = 'Tantia', last_name = 'Tope' WHERE id = 1 AND first_name = 'John';
Delete Queries¶
-
Delete a row with a condition
DELETE FROM students WHERE id = 1 AND first_name = 'John';
-
Delete a multiple rows
DELETE FROM students WHERE id IN (1, 2, 3);
Joining Queries¶
-
Get first name and last name of all students and their batch names
2. Get first name and last name of all students and their instructor namesSELECT students.first_name, students.last_name, batches.name FROM students JOIN batches ON students.batch_id = batches.id;
SELECT s.first_name, s.last_name, i.first_name, b.name, i.last_name FROM students s JOIN batches b ON s.batch_id = b.id JOIN instructors i ON b.instructor_id = i.id;