0% found this document useful (0 votes)
31 views3 pages

Chapter 15 Grouping and Joins in SQL

The document discusses different types of SQL functions including scalar and aggregate functions, using GROUP BY to arrange data into groups, filtering grouped rows with the HAVING clause, performing joins between tables to combine records that meet join conditions specified in the WHERE clause, and using table aliases to reference tables with shorter names in queries. It provides examples of queries using functions like COUNT, GROUP BY, HAVING, different join types, and table aliases.

Uploaded by

Uma TNA
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)
31 views3 pages

Chapter 15 Grouping and Joins in SQL

The document discusses different types of SQL functions including scalar and aggregate functions, using GROUP BY to arrange data into groups, filtering grouped rows with the HAVING clause, performing joins between tables to combine records that meet join conditions specified in the WHERE clause, and using table aliases to reference tables with shorter names in queries. It provides examples of queries using functions like COUNT, GROUP BY, HAVING, different join types, and table aliases.

Uploaded by

Uma TNA
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/ 3

Chapter 15 – Grouping Records, Joins in SQL

Types of SQL Function:

SQL supports many Functions. All these functions can be generally categorized in to two Types.

1. Scalar / Single Row functions

Scalar functions are used to perform calculations on a single value and return a single result.

Eg : LENGTH() - Returns the number of characters in a string


UPPER () - Converts a string to uppercase

2. Aggregate / Group / Multiple Row functions

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:

SELECT column_name(s) FROM table_name GROUP BY column_name(s);

Eg :

mysql> SELECT * FROM SALE;

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

< Query Result>


Having Clause:

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

Row functions Column functions

Select, update and delete statements Only select statement

Applied before GROUP BY clause Used after 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:

SELECT column_name(s) FROM table_name1 JOIN table_name2;

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

mysql> SELECT Name,Class FROM DANCE JOIN ON MUSIC;

(Alternative)

mysql> SELECT Name,Class FROM DANCE, MUSIC;


Cartesian Product:

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’

With Where Class:

mysql> SELECT * FROM DANCE D, MUSIC M WHERE D.Name = M.Name;

Table Aliases:

Aliases are used to address database tables with a shorter or more meaningful name within an SQL query.

 Table alias is valid only for current query.


 The original table name cannot be used in the query if its alias is given in FROM clause

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.

Eg: Uniform table (Ucode – Primary key)


+-------+-------+--------+
| Ucode | Uname | Ucolor |
+-------+-------+--------+
| 1 | Shirt | White |
| 2 | Pant | Grey |
| 3 | Tie | Blue |
+-------+-------+--------

Cost table (Ucode – Foregin key)


+-------+------+-------+
| Ucode | Size | Price |
+-------+------+-------+
|1| L | 580 |
|1| M | 500 |
|2| L | 890 |
|2| M | 810 |
+-------+------+-------

Using Where:
mysql> SELECT * FROM UNIFORM U, COST C WHERE U.UCode = C.UCode;

Using Join:

mysql> SELECT * FROM UNIFORM U JOIN COST C ON U.Ucode=C.Ucode;

Using Natural Join:

mysql> SELECT * FROM UNIFORM NATURAL JOIN COST;

You might also like