-
Notifications
You must be signed in to change notification settings - Fork 0
/
person.sql
88 lines (59 loc) · 2.49 KB
/
person.sql
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
-- 1: Create a table called person that records a person’s id, name, age, height (in cm , city, favorite_color. id should be an auto-incrementing id/primary key (use type: SERIAL)
CREATE TABLE person (
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT,
height FLOAT NOT NULL,
city VARCHAR(50) NOT NULL,
favorite_color VARCHAR(30)
);
-- 2: Add 5 different people into the person database. Remember to not include the person_id because it should auto-increment.
INSERT INTO person (name, age, height, city, favorite_color)
VALUES ('Abby', 18, 162.0, 'Missouri City', 'Purple'),
('Barbara', 36, 153.0, 'Kansas City', 'Blue'),
('Cris', 62, 174.0, 'Philadelphia', 'Red'),
('Darby', 25, 148.0, 'Phoenix', 'Green'),
('Ernie', 75, 165.0, 'Los Angeles', 'Yellow');
SELECT * FROM person;
-- 3: Select all the people in the person table by height from tallest to shortest.
SELECT * FROM person
ORDER BY height DESC;
-- 4: Select all the people in the person table by height from shortest to tallest.
SELECT * FROM person
ORDER BY height ASC;
-- 5: Select all the people in the person table by age from oldest to youngest.
SELECT * FROM person
ORDER BY age DESC;
-- 6: Select all the people in the person table older than age 20.
SELECT * FROM person
WHERE age > 20;
-- 7: Select all the people in the person table that are exactly 18.
SELECT * FROM person
WHERE age = 18;
-- 8: Select all the people in the person table that are less than 20 and older than 30.
SELECT * FROM person
WHERE age < 20;
SELECT * FROM person
WHERE age > 30;
-- 9: Select all the people in the person table that are not 27 (use not equals).
SELECT * FROM person
WHERE age != 27;
-- 10: Select all the people in the person table where their favorite color is not red.
SELECT * FROM person
WHERE favorite_color != 'Red';
-- Select all the people in the person table where their favorite color is not red and is not blue.
SELECT * FROM person
WHERE favorite_color != 'Red'
AND favorite_color != 'Blue';
-- Select all the people in the person table where their favorite color is orange or green.
SELECT * FROM person
WHERE favorite_color = 'Green'
OR favorite_color = 'Orange';
-- Select all the people in the person table where their favorite color is orange, green or blue (use IN).
SELECT * FROM person
WHERE favorite_color
IN ('Green', 'Orange', 'Blue');
-- Select all the people in the person table where their favorite color is yellow or purple (use IN).
SELECT * FROM person
WHERE favorite_color
IN ('Yellow', 'Purple');