Minggu, 16 Oktober 2011

Resume SQL1 Pertemuan 5

JOIN & FUNGSI-FUNGSI AGGREGATE

SQL JOIN

The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables.

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.

Look at the "Persons" 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.

Next, we have the "Orders" table:

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: 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

SQL INNER JOIN Keyword

The INNER JOIN keyword return rows when there is at least one match in both tables.

SQL INNER JOIN Syntax

PS: INNER JOIN is the same as JOIN.

SQL INNER JOIN Example

The "Persons" table:


The "Orders" table:

Now we want to list all the persons with any orders.

We use the following SELECT statement:
 
 The result-set will look like this:

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.

SQL LEFT JOIN Keyword

The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2).

SQL LEFT JOIN Syntax


PS: In some databases LEFT JOIN is called LEFT OUTER JOIN.

SQL LEFT JOIN Example

The "Persons" table:

The "Orders" table:

Now we want to list all the persons and their orders - if any, from the tables above.

We use the following SELECT statement:

The result-set will look like this:

The LEFT JOIN keyword returns all the rows from the left table (Persons), even if there are no matches in the right table (Orders).

SQL RIGHT JOIN Keyword

The RIGHT JOIN keyword returns all the rows from the right table (table_name2), even if there are no matches in the left table (table_name1).

SQL RIGHT JOIN Syntax

PS: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.

SQL RIGHT JOIN Example

The "Persons" table:

The "Orders" table:

Now we want to list all the orders with containing persons - if any, from the tables above.

We use the following SELECT statement:

The result-set will look like this:

The RIGHT JOIN keyword returns all the rows from the right table (Orders), even if there are no matches in the left table (Persons).

SQL FULL JOIN Keyword

The FULL JOIN keyword return rows when there is a match in one of the tables.

SQL FULL JOIN Syntax

SQL FULL JOIN Example

The "Persons" table:

The "Orders" table:

Now we want to list all the persons and their orders, and all the orders with their persons.

We use the following SELECT statement:

The result-set will look like this:

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.

SQL COUNT(column_name) Syntax

The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column:

SQL COUNT(*) Syntax
The COUNT(*) function returns the number of records in a table:

SQL COUNT(DISTINCT column_name) Syntax
The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column:

Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with Microsoft Access.

SQL COUNT(column_name) Example

We have the following "Orders" table:

Now we want to count the number of orders from "Customer Nilsen".

We use the following SQL statement:

The result of the SQL statement above will be 2, because the customer Nilsen has made 2 orders in total:

SQL COUNT(*) Example
If we omit the WHERE clause, like this:

The result-set will look like this:

which is the total number of rows in the table.

SQL COUNT(DISTINCT column_name) Example

Now we want to count the number of unique customers in the "Orders" table.
We use the following SQL statement:

The result-set will look like this:

which is the number of unique customers (Hansen, Nilsen, and Jensen) in the "Orders" table.

The AVG() Function

The AVG() function returns the average value of a numeric column.

SQL AVG() Syntax


SQL AVG() Example
We have the following "Orders" table:

Now we want to find the average value of the "OrderPrice" fields.
We use the following SQL statement:

The result-set will look like this:

Now we want to find the customers that have an OrderPrice value higher than the average OrderPrice value.
We use the following SQL statement:

The result-set will look like this:


The MIN() Function

The MIN() function returns the smallest value of the selected column.
SQL MIN() Syntax

SQL MIN() Example

We have the following "Orders" table:

Now we want to find the smallest value of the "OrderPrice" column.
We use the following SQL statement:

The result-set will look like this:

The MAX() Function

The MAX() function returns the largest value of the selected column.
SQL MAX() Syntax

SQL MAX() Example

We have the following "Orders" table:

Now we want to find the largest value of the "OrderPrice" column.
We use the following SQL statement:

The result-set will look like this:

The SQL SELECT DISTINCT Statement

In a table, some of the columns may contain duplicate values. This is not a problem, however, sometimes you will want to list only the different (distinct) values in a table.
The DISTINCT keyword can be used to return only distinct (different) values.

SQL SELECT DISTINCT Syntax

SELECT DISTINCT Example

The "Persons" table:

Now we want to select only the distinct values from the column named "City" from the table above.
We use the following SELECT statement:

The result-set will look like this:

The GROUP BY Statement

The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

SQL GROUP BY Syntax

SQL GROUP BY Example

We have the following "Orders" table:

Now we want to find the total sum (total order) of each customer.
We will have to use the GROUP BY statement to group the customers.
We use the following SQL statement:

The result-set will look like this:

Nice! Isn't it? :)
Let's see what happens if we omit the GROUP BY statement:

The result-set will look like this:

The result-set above is not what we wanted.
Explanation of why the above SELECT statement cannot be used: The SELECT statement above has two columns specified (Customer and SUM(OrderPrice). The "SUM(OrderPrice)" returns a single value (that is the total sum of the "OrderPrice" column), while "Customer" returns 6 values (one value for each row in the "Orders" table). This will therefore not give us the correct result. However, you have seen that the GROUP BY statement solves this problem.

GROUP BY More Than One Column

We can also use the GROUP BY statement on more than one column, like this:

The HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

SQL HAVING Syntax

SQL HAVING Example

We have the following "Orders" table:

Now we want to find if any of the customers have a total order of less than 2000.
We use the following SQL statement:

The result-set will look like this:

Now we want to find if the customers "Hansen" or "Jensen" have a total order of more than 1500.
We add an ordinary WHERE clause to the SQL statement:




 The result-set will look like this:



STIKOM SURABAYA
Riko Dwi Christian
D3 - Manajemen Informatika

0 komentar:

Posting Komentar