DBMS Lab # 7 Joins
DBMS Lab # 7 Joins
SQL Joins
SQL JOIN
SQL joins are used to query data from two or more tables, based on a relationship between
certain columns in these tables. The JOIN keyword is used for this purpose. Tables in a
database are often related to each other with keys. A primary key is a column (or a
combination of columns) with a unique value for each row. Each primary key value must be
unique within the table. The purpose is to bind data together, across tables, without repeating
all of the data in every table.
Note that the "P_Id" column is the primary key in the "Persons" table. This means that no
two rows can have the same P_Id. The P_Id distinguishes two persons even if they have the
same name.
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764
Note that the "O_Id" column is the primary key in the "Orders" table and that the "P_Id"
column refers to the persons in the "Persons" table without using their names.
Notice that the relationship between the two tables above is the "P_Id" column.
Different SQL JOINs
Before we continue with examples, we will list the types of JOIN you can use, and the
differences between them.
• JOIN/ INNER JOIN/ SIMPLE JOIN: Return rows when there is at least one match
in both tables
• LEFT JOIN: Return all rows from the left table, even if there are no matches in the
right table
• RIGHT JOIN: Return all rows from the right table, even if there are no matches in the
left table
• FULL JOIN: Return rows when there is a match in one of the tables
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764
The INNER JOIN keyword return rows when there is at least one match in both tables. If
there are rows in "Persons" that do not have matches in "Orders", those rows will NOT be
listed
SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764
Now we want to list all the persons and their orders - if any, from the tables above.
Svendson Tove
The LEFT JOIN keyword returns all the rows from the left table (Persons), even if there are no
matches in the right table (Orders).
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764
Now we want to list all the orders with containing persons - if any, from the tables above.
34764
The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if there are
no matches in the left table (Persons).
SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764
Now we want to list all the persons and their orders, and all the orders with their persons.
Svendson Tove
34764
The FULL JOIN keyword returns all the rows from the left table (Persons), and all the rows
from the right table (Orders). If there are rows in "Persons" that do not have matches in
"Orders", or if there are rows in "Orders" that do not have matches in "Persons", those rows
will be listed as well.
Lab Tasks:
1 2008/11/12 1000 3
2 2008/10/23 1600 3
3 2008/09/02 700 1
4 2008/09/03 300 1
5 2008/08/30 2000 1
Lab Task 2:
Give the details of persons who had max order.
Give the details of persons who had an order price greater than 1000.
Give the details of persons who ordered in September.
Give the total price of all orders placed by Pettersen.
Assignment: (should be submitted in hard form)
Deadline: 4th April 2023
Substring
Ltrim
Charindex
Rtrim
Replace
Concatenating two columns
Add constraint
Over
Convert
Also write SQL query for each of the above.
Write an SQL Query to copy a complete column from one table to another table’s
column by updating the previous records as well as inserting new records by giving an
example with sample tables.
Write an SQL Query to copy a complete table to another table by giving an example
with sample tables.
Write an SQL Query to copy data from one column to another column in the same table
by giving an example with sample tables.