Chapter 15 Grouping and Joins in SQL
Chapter 15 Grouping and Joins in SQL
SQL supports many Functions. All these functions can be generally categorized in to two Types.
Scalar functions are used to perform calculations on a single value and return a single result.
Aggregate functions are used to perform calculations on a set of values and return a single
result. Eg: SUM () - Returns the sum of all non-NULL values in a column
MAX () - Returns the maximum value in a column
Group By:
The SQL GROUP BY clause is used in conjunction with the SELECT statement to arrange identical data into
Groups.
Syntax:
Eg :
+---------+------+-------+------------+-------------+------+----------+----------+ -------------+
|InvoiceNo|CarId |CustId | SaleDate | PaymentMode |EmpID |SalePrice |
+---------+------+-------+------------+-------------+------+----------+----------+--------------+
| I00001 | D001 | C0001 | 2019-01-24 | Credit Card | E004 |613248.00 |
| I00002 | S001 | C0002 | 2018-12-12 | Online | E004 |590321.00 |
| I00003 | S002 | C0004 | 2019-01-25 | Cheque | E004 |604000.00 |
| I00004 | D002 | C0001 | 2018-10-15 | Bank Finance | E007 |659982.00 |
| I00005 | E001 | C0003 | 2018-12-20 | Credit Card | E002 |369310.00 |
| I00006 | S002 | C0002 | 2019-01-30 | Bank Finance | E007 |620214.00 |
--------------------------------------------------------------------------------------------------------
1. Display the number of Cars purchased by each Customer from SALE table.
mysql> SELECT CustID, COUNT(*) "Number of Cars" FROM SALE GROUP BY CustID;
< Query Result>
2. Display the number of Cars Sold by each Employee from SALE table.
<Write Query and result>
3. Display the number of Cars Sold with paymantmode from SALE table.
<Write Query and result>
4. Display the PaymentMode and number of payments made using that mode more than once.
mysql> SELECT PaymentMode, Count(PaymentMode) FROM SALE GROUP BY Paymentmode
HAVING COUNT(*)>1 ORDER BY Paymentmode
SQL HAVING clause is similar to the WHERE clause; they are both used to filter rows in a table based
on conditions. However, the HAVING clause was included in SQL to filter grouped rows instead of single rows.
WHERE vs HAVING
Where Clause in SQL Having Clause in SQL
Applicable without GROUP BY clause Does not function without GROUP BY clause
Joins:
The SQL Joins clause is used to combine records from two or more tables in a database.
A Join Clause works with respect to a join-predicate. This join-predicate is specified in a WHERE clause; so it
is nothing but a condition that must be satisfied by database tables in order to combine them.
Syntax:
Ex:
DANCE
+------+--------+-------+
| SNo | Name | Class |
+------+--------+-------+
| 1 | Aastha | 7A |
| 2 | Mahira | 6A |
| 3 | Mohit | 7B |
| 4 | Sanjay | 7A |
+------+--------+-------+
MUSIC
+------+---------+-------+
| SNo | Name | Class |
+------+---------+-------+
| 1 | Mehak | 8A |
| 2 | Mahira | 6A |
| 3 | Lavanya | 7A |
| 4 | Sanjay | 7A |
| 5 | Abhay | 8A |
+------+---------+-------
(Alternative)
Cartesian Product (X) Cartesian product operation combines tuples from two relations. It results in all pairs of
rows from the two input relations, regardless of whether or not they have the same values on common
attributes. It is denoted as ‘X’
Table Aliases:
Aliases are used to address database tables with a shorter or more meaningful name within an SQL query.
JOIN operation combines tuples from two tables on specified conditions. This is unlike cartesian product
which make all possible combinations of tuples. While using the JOIN clause of SQL, we specify
conditions on the related attributes of two tables within the FROM clause.
Using Where:
mysql> SELECT * FROM UNIFORM U, COST C WHERE U.UCode = C.UCode;
Using Join: