0% found this document useful (0 votes)
13 views19 pages

Meet 11

This document provides an overview of advanced SQL topics including inner joins, left and right joins, cross joins, unions, and subqueries. It includes objectives, examples of multi-table queries using different join types, and explanations of unions and subqueries.

Uploaded by

Sarah Harahap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views19 pages

Meet 11

This document provides an overview of advanced SQL topics including inner joins, left and right joins, cross joins, unions, and subqueries. It includes objectives, examples of multi-table queries using different join types, and explanations of unions and subqueries.

Uploaded by

Sarah Harahap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

2024

Advanced SQL
SI2304 – Pemodelan Basis Data
BACK COURSE TOPIC

Overview
• Advanced SQL is a query that studies in more detail about INNER JOIN, LEFT
JOIN/LEFT OUTER JOIN, RIGHT JOIN/RIGHT OUTER JOIN, CROSS JOIN, UNION,
Subquery.
• A database schema is a sketch or diagram that refers to the arrangement or organization of
data stored in a database. There are two types of schema, namely logical and physical. The
first type represents how the data is organized, while the second determines its physical
storage structure.
• The database schema is very important in the process of creating and managing a database
system, because it helps users understand and read the information stored in the database.
BACK COURSE TOPIC

Objectives
Able to build queries to display data in a single table correctly.
Able to describe and differentiate union and how to use them appropriately
BACK COURSE TOPIC

Multi Table Query


Basic Query: only involves 1 table, often called a Query Single Table
Advanced Query: involving more than 1 table, often called a Multi Table Query

Some common syntax used to connect more than 1 table are:


• INNER JOINS
• LEFT JOIN/LEFT OUTER JOIN
• RIGHT JOIN/RIGHT OUTER JOIN
• CROSS JOINS
• UNIONS
BACK COURSE TOPIC

Multi Table Query


Sample Database: Academic
study_program (study code, study name, study location)
student (name, student's name, study program code, student's
address, student's date of birth)

Inner Join

• Most commonly used


• Default join type
• Usually between 2 tables
• Can also be used for more than 2 tables
BACK COURSE TOPIC

Ex : Multi Table Query


Inner Join – Example
` Display the student's name along with the name of their study program `

SELECT student name, study program name


FROM study_program INNER JOIN students
ON study_program.Prodicode = student.Prodicode;

or:

SELECT student name, study program name


FROM study_program t1 INNER JOIN student t2
ON t1.Prodicode = t2.Prodicode;
BACK COURSE TOPIC

Ex : Multi Table Query


Left Join

• All contents of the left table are displayed


• The contents of the right table are displayed only in
accordance with the contents of the left table
• Usually between 2 tables
• Can also be used for more than 2 tables
BACK COURSE TOPIC

Ex : Multi Table Query


Left Join – Example
`Display all fields from the student table left joined with the study program table`

SELECT *
FROM students t1 LEFT JOIN study_program t2
ON t1.Prodicode = t2.Prodicode;

or:

SELECT *
FROM students t1 LEFT OUTER JOIN study_program t2
ON t1.Prodicode = t2.Prodicode;
BACK COURSE TOPIC

Ex : Multi Table Query


Right Join

• All contents of the right table are displayed


• The contents of the left table displayed are only those that
correspond to the contents of the right table
• Usually between 2 tables
• Can also be used for more than 2 tables
BACK COURSE TOPIC

Ex : Multi Table Query


Right Join – Example
` Display all fields from the student table right joined with the study program table`

SELECT *
FROM students t1 RIGHT JOIN study_program t2
ON t1.Prodicode = t2.Prodicode;

or:

SELECT *
FROM students t1 RIGHT OUTER JOIN study_program t2
ON t1.Prodicode = t2.Prodicode;
BACK COURSE TOPIC

Ex : Multi Table Query


Cross Join

• Cartesian Product Join


• Each record in the left table will be paired with each record
in the right table
• Does not require the word ON in the query

Cross Join – Example


` Display all fields from the student table cross joined with the study program table`

SELECT *
FROM students CROSS JOIN study_program;
BACK COURSE TOPIC

Union
The set operation in SQL is an implementation of the mathematical operations or it called set operator, which
is an operation on the result of two SELECT queries. There are several types of Operator sets:
• Union
• Union All
• Intersect
• Minus

UNION is an operator used to combine query results, provided that the number, name and type of columns in
each table where the data will be displayed must be the same, where if there is duplicate data, the duplicate
data will be removed from the query results.

The method is as in the following image


BACK COURSE TOPIC

Union
The set operation in SQL is an implementation of the mathematical operations or it called set operator, which
is an operation on the result of two SELECT queries. There are several types of Operator sets:
• Union
• Union All
• Intersect
• Minus

UNION is an operator used to combine query results, provided that the number, name and type of columns in
each table where the data will be displayed must be the same, where if there is duplicate data, the duplicate
data will be removed from the query results.
BACK COURSE TOPIC

Union
The set operation in SQL is an implementation of the mathematical operations or it called set operator, which
is an operation on the result of two SELECT queries. There are several types of Operator sets:
• Union
• Union All
• Intersect
• Minus

UNION is an operator used to combine query results, provided that the number, name and type of columns in
each table where the data will be displayed must be the same, where if there is duplicate data, the duplicate
data will be removed from the query results.
BACK COURSE TOPIC

Union
The method is as in the following image:

This means that the query results from Guest will be combined with the Email query. But there is the same data
from both tables, later the same data will be combined into one like the following image.

We get all emails from both tables and there is no duplicate or same data, so that's the function of Union.
BACK COURSE TOPIC

Union All
UNION ALL is the same operation as UNION, but the duplicate data will still be displayed in the query results. The
method is the same as Union, you only need to add the word <ALL> after <UNION> as in the following example:

As a result, duplicate data will still be displayed or displayed as shown in the following image:

It can be seen that there are two duplicate data, so that is the function of UNION ALL
BACK COURSE TOPIC

Subquery
• Subquery means a query within a query. By using a subquery, the results of the query will
become part of the query above it. The subquery is located within the WHERE or HAVING
clause.

• In the WHERE clause, the subquery is used to select certain rows which are then used by the
query. Meanwhile, in the HAVING clause, the subquery is used to select a group of rows
which are then used by the query.

• Example: command to display data in the film type table where the data in the type column is
listed in the film table using IN:
BACK COURSE TOPIC

Objectives
SELECT * FROM FILM TYPE WHERE TYPE IN (SELECT TYPE FROM FILM);
or use EXISTS

SELECT * FROM FILM TYPES WHERE EXISTS (SELECT * FROM FILM WHERE PRICES
> 2000);

In the example above:


SELECT TYPE FROM FILM is called a subquery, whereas:
SELECT * FROM FILM TYPE acts as a query. Please note, there is type and price data in the film type
table that is not displayed. This is because the data in the type column is not contained in the type
column in the film table.
2024

Any Question?

BACK 2024

You might also like