forked from Ankit-Kum/DBMS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBMS_8.sql
More file actions
107 lines (89 loc) · 2 KB
/
Copy pathDBMS_8.sql
File metadata and controls
107 lines (89 loc) · 2 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
## INNER JOIN clause
SELECT
select_list
FROM t1
INNER JOIN t2 ON join_condition1
INNER JOIN t3 ON join_condition2
...;
----
# Suppose you want to get:
-- The productCode and productName from the products table.
-- The textDescription of product lines from the productlines table.
SELECT
productCode,
productName,
textDescription
FROM
products t1
INNER JOIN productlines t2
ON t1.productline = t2.productline;
-- 0r
SELECT
productCode,
productName,
textDescription
FROM
products
INNER JOIN productlines USING (productline);
# This query returns the order number, order status, and total sales from the orders and orderdetails tables using the INNER JOIN clause with the GROUP BYclause:
SELECT
t1.orderNumber,
t1.status,
SUM(quantityOrdered * priceEach) total
FROM
orders t1
INNER JOIN orderdetails t2
ON t1.orderNumber = t2.orderNumber
-- INNER JOIN orderdetails USING (orderNumber)
GROUP BY t1.orderNumber;
-- join three tables example
# See the following products, orders and orderdetails tables:
SELECT
orderNumber,
orderDate,
orderLineNumber,
productName,
quantityOrdered,
priceEach
FROM
orders
INNER JOIN
orderdetails USING (orderNumber)
INNER JOIN
products USING (productCode)
ORDER BY
orderNumber,
orderLineNumber;
-- See the following orders, orderdetails, customers and products tables:
SELECT
orderNumber,
orderDate,
customerName,
orderLineNumber,
productName,
quantityOrdered,
priceEach
FROM
orders
INNER JOIN orderdetails
USING (orderNumber)
INNER JOIN products
USING (productCode)
INNER JOIN customers
USING (customerNumber)
ORDER BY
orderNumber,
orderLineNumber;
-------------------------
# LEFT JOIN to find customers without any orders:
SELECT
c.customerNumber,
c.customerName,
o.orderNumber,
o.status
FROM
customers c
LEFT JOIN orders o
ON c.customerNumber = o.customerNumber
WHERE
orderNumber IS NULL;