4.8 KiB
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
SELECT first_name AS "Student Name" FROM students;
-
Get all unique addresses of all students
SELECT DISTINCT address FROM students;
-
Get all students with ID equal to 1
SELECT * FROM students WHERE id = 1;
-
Get all students with IQ greater than 150
SELECT * FROM students WHERE iq > 150;
-
Get all students with IQ less than 100
SELECT * FROM students WHERE iq < 100;
-
Get all students with IQ greater than 100 and less than150
SELECT * FROM students WHERE iq > 100 AND iq < 150;
-
Get all students with IQ greater than 100 or less than 150
SELECT * FROM students WHERE iq BETWEEN 100 AND 150;
-
Get all students with first name
Tantia
SELECT * FROM students WHERE first_name = 'Tantia';
-
Get all students with first name
Tantia
and last nameTope
SELECT * FROM students WHERE first_name = 'Tantia' AND last_name = 'Tope';
-
Get all students with first name
John
or first nameMycroft
SELECT * FROM students WHERE first_name = 'John' OR first_name = 'Mycroft';
-
Get all students with name
John Watson
orMycroft Holmes
SELECT * FROM students WHERE (first_name = 'John' AND last_name = 'Watson') OR (first_name = 'Mycroft' AND last_name = 'Holmes');
-
Get all students without the first name
John
SELECT * FROM students WHERE first_name <> 'John';
-
Get all students without the first name
John
or last nameMycroft
SELECT * FROM students WHERE first_name <> 'John' AND last_name <> 'Mycroft';
-
Get all students with first name starting with
T
SELECT * FROM students WHERE first_name LIKE 'T%';
-
Get all students with last name ending with
walker
SELECT * FROM students WHERE last_name LIKE '%walker';
-
Get all students with first name containing
T
SELECT * FROM students WHERE first_name LIKE '%T%';
-
Get all students with last name in the format
___walker
SELECT * FROM students WHERE last_name LIKE '___walker';
-
Get all students in Jhansi and London
SELECT * FROM students WHERE address IN ('Jhansi', 'London');
-
Get all students which do not have a batch id
SELECT * FROM students WHERE batch_id IS NULL;
-
Get the first 5 students
SELECT * FROM students LIMIT 5;
-
Get the first 5 students sorted by IQ
SELECT * FROM students ORDER BY iq LIMIT 5;
-
Get the first 5 students sorted by IQ in descending order
SELECT * FROM students ORDER BY iq DESC LIMIT 5;
-
Get the first 5 students sorted by IQ in descending order and then by first name
SELECT * FROM students ORDER BY iq DESC, first_name LIMIT 5;
Update Queries
-
Update a row
UPDATE students SET first_name = 'Tantia' WHERE id = 1;
-
Update a row with a condition
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
SELECT students.first_name, students.last_name, batches.name FROM students JOIN batches ON students.batch_id = batches.id;
-
Get first name and last name of all students and their instructor names
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;