-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice-query-1-salesDb.sql
66 lines (50 loc) · 1.71 KB
/
practice-query-1-salesDb.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
SELECT * FROM Customers;
SELECT * FROM Customers
WHERE City = 'cincinnati'
order by Name;
SELECT Name, City, State, Sales, Active FROM Customers;
SELECT * FROM Customers
order by City, Sales desc;
SELECT * FROM Customers
WHERE city = 'cleveland' AND Sales >= '50000'
order by Sales;
select * From Customers
where (City = 'cincinnati' and Sales > 30000)
or (city = 'columbus' and Sales > 35000)
or (City = 'cleveland' and Sales < 10000);
select * from Customers
where city != 'cincinnati';
-- where not (city = 'cincinnati';
-- either way works for SQL, != more recognized in programming
select * from Customers
where not (City = 'cleveland' or City = 'columbus');
-- same as city = 'cleveland' or city = 'columbus
select * from Customers
where City in ('cleveland','columbus');
SELECT * FROM OrderLines;
SELECT * FROM Orders;
-- joining 2 tables
select o.Id, c.Name, o.Date, o.Description -- gives all columns from Orders, Name column from Customers
from Orders o -- o and c are aliases for Orders and Customers
join Customers c
on o.CustomerId = c.Id;
-- group by, count function
select c.Name 'Customers', COUNT(*) as 'Orders' -- '' after column names will name the display column
from Customers c
join Orders o
on o.CustomerId = c.Id
group by c.Name
order by Orders desc;
select c.Name 'Customers', COUNT(*) as 'Orders' -- '' after column names will name the display column
from Customers c
join Orders o
on o.CustomerId = c.Id
group by c.Name
having COUNT(*) > 1
order by Orders desc;
select * from Customers
where Sales between 10000 and 30000;
-- Where Sales >= 10000 and sales <= 30000;
select * from Customers
where Sales < (select AVG(sales) from Customers)
order by Sales desc;