-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComp_Data_1_Part6
More file actions
56 lines (40 loc) · 1.37 KB
/
Comp_Data_1_Part6
File metadata and controls
56 lines (40 loc) · 1.37 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
Hi. We are going to see UNION, JOIN in this file.
##### At first, let's look at UNION--
----Find a list of employees and branch names----
SELECT first_name AS employee_branch_names
FROM employee
UNION
SELECT branch_name
FROM branch;
-----Find a list of all clients and branch suppliers' names and their IDs----
SELECT client_name AS client_branch_suppliers, client.branch_id AS ID
FROM client
UNION
SELECT supplier_name, branch_supplier.branch_id
FROM branch_supplier;
-----Find a list of all money spent or earned by the company-----
SELECT salary AS money_spent_money_earned
FROM employee
UNION
SELECT total_sales
FROM works_with;
##### Now, we will see the use of JOIN. JOIN is basically used to combine two or more rows from different tables based on their
related columns.
At first, run the following command so that you understand the difference--
INSERT INTO branch VALUES(4,'Buffalo',NULL,NULL);
###INNER JOIN-
----Find all branches and the names of their managers---
SELECT employee.emp_id, employee.first_name, branch.branch_name
FROM employee
JOIN branch
ON employee.emp_id=branch.mgr_id;
###LEFT JOIN-
SELECT employee.emp_id, employee.first_name, branch.branch_name
FROM employee
LEFT JOIN branch
ON employee.emp_id=branch.mgr_id;
###RIGHT JOIN-
SELECT employee.emp_id, employee.first_name, branch.branch_name
FROM employee
RIGHT JOIN branch
ON employee.emp_id=branch.mgr_id;