-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask 3.sql
More file actions
69 lines (54 loc) · 1.35 KB
/
Copy pathTask 3.sql
File metadata and controls
69 lines (54 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
CREATE DATABASE task3;
USE task3;
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
age INT,
email VARCHAR(100),
department VARCHAR(50)
);
INSERT INTO students (name, age, email, department) VALUES
('Vijay', 20, 'vijay@gmail.com', 'CSE'),
('Arun', 22, 'arun@yahoo.com', 'ECE'),
('Priya', 21, 'priya@gmail.com', 'CSE'),
('Karthik', 23, NULL, 'MECH'),
('Anu', 19, 'anu@gmail.com', 'IT');
-- Query to select all students with a gmail.com email address
1-- WHERE clause (filter by age)
SELECT * FROM students
WHERE age > 20;
2️-- AND / OR
-- AND
SELECT * FROM students
WHERE age > 20 AND department = 'CSE';
-- OR
SELECT * FROM students
WHERE department = 'IT' OR department = 'MECH';
3️--LIKE with wildcards
-- Names starting with A
SELECT * FROM students
WHERE name LIKE 'A%';
-- Emails containing gmail
SELECT * FROM students
WHERE email LIKE '%gmail%';
4️-- IN and BETWEEN
-- IN
SELECT * FROM students
WHERE department IN ('CSE', 'IT');
-- BETWEEN
SELECT * FROM students
WHERE age BETWEEN 20 AND 22;
5️-- IS NULL
SELECT * FROM students
WHERE email IS NULL;
6️-- Query Execution Order (concept demo)
SELECT name, age
FROM students
WHERE age > 20
ORDER BY age;
7️-- Aliases
SELECT name AS student_name, department AS dept
FROM students;
8️--Email search
SELECT * FROM students
WHERE email LIKE '%@gmail.com';