Meet 11
Meet 11
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
Inner Join
or:
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
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
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.
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);
Any Question?
BACK 2024