-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_count_campground.sql
80 lines (77 loc) · 1.48 KB
/
03_count_campground.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
/*
Goal: learn how to COUNT
We will move from the camp_example -> campground table
Since it has a complete list of US national park campgrounds
COUNT(1) means count the number of rows returned by the query
- COUNT is an aggregate function that operates on a set of rows
- Without a WHERE clause, it counts all rows in the table
*/
SELECT
COUNT(1)
FROM
camp_example;
-- result
-- +----------+
-- | COUNT(1) |
-- +----------+
-- | 3 |
-- +----------+
/*
With a WHERE clause, COUNT returns number the rows that satisfy the condition
For example there is 1 row where is_rv_allowed is NULL
*/
SELECT
COUNT(1)
FROM
camp_example
WHERE
is_rv_allowed IS NULL;
-- result
-- +----------+
-- | COUNT(1) |
-- +----------+
-- | 1 |
-- +----------+
/*
There are 2 rows where is_rv_allowed is not NULL
*/
SELECT
COUNT(1)
FROM
camp_example
WHERE
is_rv_allowed IS NOT NULL;
-- result
-- +----------+
-- | COUNT(1) |
-- +----------+
-- | 2 |
-- +----------+
/*
We will now swith to use the campground table for the rest of the queries
There are 638 rows in the campground table
*/
SELECT
COUNT(1)
FROM
campground;
-- result
-- +----------+
-- | COUNT(1) |
-- +----------+
-- | 638 |
-- +----------+
/*
COUNT(*) usually is a synonym for COUNT(1)
- Technically for some databases it means count the number of rows with any non-null value
*/
SELECT
COUNT(*)
FROM
campground;
-- result
-- +----------+
-- | COUNT(*) |
-- +----------+
-- | 638 |
-- +----------+