0% found this document useful (0 votes)
158 views

Using Basic Structured Query Language

This document provides information about using basic structured query language (SQL) to define, create, and manipulate database structures and data in a relational database. It begins with learning outcomes, then provides details about database management systems (DBMS), including definitions of a database, purposes of a DBMS, components of a DBMS like storage engines and query languages, and examples of using data definition language and data manipulation language commands in SQL.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
158 views

Using Basic Structured Query Language

This document provides information about using basic structured query language (SQL) to define, create, and manipulate database structures and data in a relational database. It begins with learning outcomes, then provides details about database management systems (DBMS), including definitions of a database, purposes of a DBMS, components of a DBMS like storage engines and query languages, and examples of using data definition language and data manipulation language commands in SQL.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Debre Markos Poly Technic College

Department of Information Technology


TVET-PROGRAMME TITLE: Web Development and Database Administration Level III
MODULE TITLE: Using Basic Structured Query Language
CODE: EIS WDDBA3 M07 0322 NOMINAL DURATION: 90 Hours

MODULE DESCRIPTION:

This module defines the competency required to use a basic structured query language (SQL) to define,
create and manipulate database structures and associated data in a relational database LEARNING
OUTCOMES At the end of the module the trainee will be able to:

LO1: Write an SQL statement to retrieve and sort data


LO2: Write SQL statements that use functions
LO3: Write SQL statements that use aggregation and filtering
LO4: Write and execute SQL sub-queries

LO1. Write an SQL statement to retrieve and sort data


1.1 Understanding DBMS fundamental

DBMS fundamental
A database management system (DBMS) is a software tool that enables users to manage a database
easily. It allows users to access and interact with the underlying data in the database. These actions can
range from simply querying data to defining database schemas that fundamentally affect the database
structure.
Furthermore, DBMS allow users to interact with a database securely and concurrently without interfering
with each user and while maintaining data integrity.

Database tasks in a DBMS


 Configuring authentication and authorization.
 Providing data backups and snapshots
 Performance tuning
 Data recovery.
What is a Database?
A database is a collection of information that is organized so that it can be easily accessed, managed, and
updated.
 end-user data-raw facts of interests to the end-user
 meta data-data about data
Database Properties
 Represents some aspects of the real world. (mini world or the universe of discourse)
 A database is a logically coherent collection of data with some inherent meaning.
 A database is designed, built, and populated with data for a specific purpose.
DBMS-Data Base Management System
DBMS is a collection of programs that enables users to create and maintain a database. It provides an
environment that is both convenient and efficient to use.

Using Basic Structured Query Language Compiled by Bantegizia.Z


Database + DBMS = Database System
# DBMS facilitates the process of,

Defining- A database involves specifying the data types, structures, and constraints for the data to
be stored in the database.
Constructing- The database is the process of storing the data itself on some storage, medium that
is controlled by the DBMS.
Manipulating- A database includes such as querying the database to retrieve specific data,
updating the database to reflect changes in the mini-world, and generating reports from the data.

Applications of DBMS
 Banking- All transactions
 Airlines- Reservations, schedules
 Universities- Registrations, grades
 Sales- Customers, products, purchase
 Manufacturing- Production, inventory, orders, supply chain
Purpose of Database System
In early days database applications were built on top of the file systems.
# Drawbacks of using file systems to store data,
 Data redundancy and inconsistency — multiple file-formats, duplication of information in
different files.
 Difficulty in accessing data — need to re-write programs to carry out new tasks.
 Data isolation
 Integrity problems — hard to add or change constraints.
 Atomicity of updates — failures may leave the database in an inconsistent state with partial
updates carried out.
 Concurrent access from multiple users.
 Security problems.
Benefits of the database approach
 Potential for enforcing standards — defined for names and formats of data elements, display
formats, report structure, terminology, etc.
 Reduced application development time — development time using a DBMS is estimated to be
one-sixth to one-fourth of that for a traditional file system.
 Flexibility — easy to change the structure of a database as requirements change.
 Availability of up to date information.
Components of a database management system
All DBMS comes with various integrated components and tools necessary to carry out almost all database
management tasks. Some DBMS software even provides the ability to extend beyond the core
functionality by integrating with third-party tools and services, directly or via plugins.
In this section, we will look at the common components that are universal across all DBMS software,
including:
 Storage engine

Using Basic Structured Query Language Compiled by Bantegizia.Z


 Query language
 Query processor
 Optimization engine
 Metadata catalog
 Log manager
 Reporting and monitoring tools
 Data utilities
Storage engine
The storage engine is the core component of the DBMS that interacts with the file system at an OS level
to store data. All SQL queries which interact with the underlying data go through the storage engine.
Query language
A database access language is required for interacting with a database, from creating databases to simply
inserting or retrieving data. A proper DBMS must support one or multiple query languages and language
dialects. Structured query language (SQL) and MongoDB Query Language (MQL) are two query
languages that are used to interact with the databases.
In many query languages, the query language functionality can be further categorized according to
specific tasks:
 Data Definition Language (DDL). This consists of commands that can be used to define database
schemas or modify the structure of database objects.
DDL(Data Definition Language): To make/perform changes to the physical structure of any table
residing inside a database, DDL is used. These commands when executed are auto-commit in
nature and all the changes in the table are reflected and saved immediately.
Following are the five DDL commands in SQL:
 CREATE Command
 DROP Command
 ALTER Command
 TRUNCATE Command
 RENAME Command
EXAMPLE of CREATE Command
CREATE Database Database_Name;
CREATE TABLE table_name
(
column_Name1 data_type ( size of the column ) ,
column_Name2 data_type ( size of the column) ,
column_Name3 data_type ( size of the column) ,
...
column_NameN data_type ( size of the column )
);
CREATE INDEX index_city_State ON Employee (Emp_City, Emp_State);
EXAMPLE of DROP Command

DROP DATABASE Database_Name;


DROP TABLE Table_Name;
DROP INDEX Index_Name;

Using Basic Structured Query Language Compiled by Bantegizia.Z


EXAMPLE of ALTER Command
ALTER TABLE name_of_table ADD column_name column_definition;
ALTER TABLE Student ADD Father's_Name Varchar(60);
ALTER TABLE StudentDROP Age, Marks;
ALTER TABLE table_name MODIFY ( column_name column_datatype(size));
ALTER TABLE table_name MODIFY ( Last_Name varchar(25));
EXAMPLE of TRUNCATE Command
TRUNCATE TABLE Table_Name;
TRUNCATE TABLE Student;
EXAMPLE of TRUNCATE Command
RENAME TABLE Student TO Student_Details ;

 Data Manipulation Language (DML). Commands that directly deal with the data in the
database. All CRUD operations come under DML.
 DML(Data Manipulation Language): Once the tables are created and the database is generated
using DDL commands, manipulation inside those tables and databases is done using DML
commands. The advantage of using DML commands is, that if in case any wrong changes or
values are made, they can be changed and rolled back easily.
 DML commands:
INSERT: It is used to insert data into a table.
INSERT INTO Student (Stu_id, Stu_Name, Stu_Marks, Stu_Age) VALUES (104, A
nmol, 89, 19);
UPDATE: It is used to update existing data within a table.
UPDATE Product SET Product_Price = 80 WHERE Product_Id = 'P102' ;
UPDATE Student SET Stu_Marks = 80, Stu_Age = 21 WHERE Stu_Id = 103 AND S
tu_Id = 202;
DELETE: It is used to delete records from a database table.
DELETE FROM Product WHERE Product_Id = 'P202' ;
DELETE FROM Student WHERE Stu_Marks > 70 ;
o LOCK: Table control concurrency.
o CALL: Call a PL/SQL or JAVA subprogram.
o EXPLAIN PLAN: It describes the access path to data.
 DQL(Data Query Language): Data query language consists of only one command upon which
data selection in SQL relies. The SELECT command in combination with other SQL clauses is
used to retrieve and fetch data from databases/tables based on certain conditions applied by the
user. The SELECT command is used in Data Query Language Command

EXAMPLE
Select * FROM dost;
Select: It is used to retrive data into a table.
SELECT * FROM table_name;
SELECT Emp_Id, Emp_Salary FROM Employee;
SELECT * FROM Student WHERE Stu_Marks = 80;

Using Basic Structured Query Language Compiled by Bantegizia.Z


 Data Control Language (DCL). This deals with the permissions and other access controls of the
database.

DCL(Data Control Language): DCL commands as the name suggests manage the matters and
issues related to the data controller in any database. DCL includes commands such
as GRANT and REVOKE which mainly deal with the rights, permissions, and other controls of
the database system.

DCL commands:
o GRANT: This command gives users access privileges to the database.
EXAMPLE
o GRANT SELECT, UPDATE ON TABLE Student TO groupx;
o GRANT INSERT ON films TO PUBLIC;
o REVOKE Insert ON films FROM PUBLIC;

 Transaction Control Language (TCL). Command which deals with internal database
transactions.

TCL(Transaction Control Language): Transaction Control Language as the name suggests


manages the issues and matters related to the transactions in any database. They are used to roll
back or commit the changes in the database.

Query processor
This is the intermediary between the user queries and the database. The query processor
interprets the queries of users and makes them actionable commands that can be understood by
the database to perform the appropriate functionality.
Optimization engine
The optimization Engine allows the DBMS to provide insights into the performance of the
database in terms of optimizing the database itself and queries. When coupled with database
monitoring tools, it can provide a powerful toolset to gain the best performance out of the
database.
Metadata catalog
This is the centralized catalog of all the objects within the database. When an object is created,
the DBMS keeps a record of that object with some metadata about it using the metadata catalog.
Then, this record can be used to:
 Verify user requests to the appropriate database objects
 Provide an overview of the complete database structure
Log manager
This component will keep all the logs of the DBMS. These logs will consist of user logins and
activity, database functions, backups and restore functions, etc. The log manager ensures all
these logs are properly recorded and easily accessible.
Reporting & monitoring tools

Using Basic Structured Query Language Compiled by Bantegizia.Z


Reporting and monitoring tools are another standard component that comes with a DBMS.
Reporting tools will enable users to generate reports while monitoring tools enable monitoring
the databases for resource consumption, user activity, etc.
Data utilities
In addition to all the above, most DBMS software comes with additional inbuilt utilities to
provide functionality such as:
 Data integrity checks
 Backup and restore
 Simple database repair
 Data validations
 Etc.
Types of database management systems
There are many different types of DBMS, yet we can categorize the most commonly used DBMS
into three types.

Relational database management systems (RDBMS)


This is the most common type of DBMS. They are used to interact with databases that contain structured
data in a table format with predefined relationships. Moreover, they use structured query language (SQL)
to interact with databases. Microsoft SQL, MySQL, and Oracle Database are some popular DBMS that
come under this category.

Document database management systems (DoDBMS)


These DoDBMS are used to manage databases that contain data stored in JSON-like structures
with limited or no relationship structure. They are powered by query languages such as
MongoDB query language (MQL) for database operations. MongoDB, Azure Cosmos DB are
some prominent examples of DoDBMS.

Columnar database management systems (CDBMS)


As the name suggests, this type of DBMS is used to manage columnar databases that store data
in columns instead of rows, emphasizing high performance. Some databases that use columnar
format are Apache Cassandra, Apache HBase, etc.

Advantages of a DBMS
DBMS was introduced to solve the fundamental issues associated with storing, managing,
accessing, securing, and auditing data in traditional file systems. Software users and
organizations can gain the following benefits by using DBMS:

Increased data security


DBMS provides the ability to control users and enforce policies for security and compliance
management. This controlled user access increases the database security and makes the data less
vulnerable to security breaches.

Using Basic Structured Query Language Compiled by Bantegizia.Z


Simple data sharing
DBMS enables users to access the database securely regardless of their location. Thus, they can
handle any database-related task promptly without the need for complex access methods or
worrying about database security. On top of that, DBMS allows multiple users to collaborate
effectively when interacting with the database.

Data integration
DBMS allows users to gain a centralized view of databases spread across multiple locations and
manage them using a single interface rather than operating them as separate entities.

Abstraction & independence


DBMS enables users to change the physical schema of a database without changing the logical
schema that governs database relationships. As a result, organizations can scale the underlying
database infrastructure without affecting the database operations.
Furthermore, any change to the logical schema can also be carried out without affecting
applications that access the databases.

Streamlined backup & recovery mechanism


Most databases have built-in backup and recovery tools. Yet, DBMS offers centralized tools to
facilitate backup and recovery functionality more conveniently and thereby provide a better user
experience. Securing data has become easier than ever with functionality like:
 Automated snapshots
 Backup scheduling
 Backup verifications
 Multiple recovery methods

Uniform management & monitoring


DBMS provides a single interface to carry out all the management and monitoring tasks, thus
simplifying the workload of database administrators. These tasks can range from database
creation and schema modifications to reporting and auditing.

DBMSs are essential


DBMS is an essential component for any organization when it comes to managing databases.
The scale, complexity, and feature set of a DBMS will depend on the specific DBMS and
requirements of the organizations.
With different DBMS providing different feature sets, it is paramount that organizations
rigorously evaluate the DBMS software before committing to a single system. However, a
properly configured DBMS will greatly simplify the management and maintenance of databases
at any scale.

Using Basic Structured Query Language Compiled by Bantegizia.Z


1.2 Identifying database information requirement
Data requirements definition establishes the process used to identify, prioritize, precisely
formulate, and validate the data needed to achieve business objectives. When documenting data
requirements, data should be referenced in business language, reusing approved standard
business terms if available. If business terms have not yet been standardized and approved for the
data within scope, the data requirements process provides the occasion to develop them.

1.3 Retrieving all data from a table following work procedure


Retrieving data from one table
In SQL, to retrieve data stored in our tables, we use the SELECT statement. The result of this
statement is always in the form of a table that we can view with our database client software or
use with programming languages to build dynamic web pages or desktop applications. While the
result may look like a table, it is not stored in the database like the named tables are. The result
of a SELECT statement can also be used as part of another statement.

Basic syntax of SELECT statement


The basic syntax consists of four clauses as shown in the figure below. While SQL is not case
sensitive, by convention many database developers use uppercase for keywords to improve
readability.

The four basic clauses of a SQL SELECT statement.


Of the four clauses, only the first two are required. The two shown in square brackets are
optional. When you start learning to build queries, it is helpful to follow a specific step-by-step
sequence, look at the data after each modification to the query, and be sure that you understand
the results at each step. This iterative refinement will allow you to hone in on just the right SQL
statement to retrieve the desired information. Below is a summary of the clauses.

The SELECT clause allows us to specify a comma-separated list of attribute names


corresponding to the columns that are to be retrieved. You can use an asterisk character, *, to
retrieve all the columns.In queries where all the data is found in one table, the FROM clause is
where we specify the name of the table from which to retrieve rows. In other articles we will use
it to retrieve rows from multiple tables.

The WHERE clause is used to constrain which rows to retrieve. We do this by specifying a
boolean predicate that compares the values of table columns to literal values or to other columns.
The ORDER BY clause gives us a way to order the display of the rows in the result of the
statement.
The example of the next section provides more information on how to retrieve information using
this SELECT statement.

SQL Example: customers in a specified zip code

Using Basic Structured Query Language Compiled by Bantegizia.Z


We’ll build a list of customers who live in a specific zip code area, showing their first and last
names and phone numbers and listing them in alphabetical order by last name. A company might
want to do this to initiate a marketing campaign to customers in this area. In this example, we’ll
use zip code 90840. Listed below are the refinement steps we take to arrive at the statement that
will retrieve what we need.
Start by retrieving all of the relevant data; in this case, that is all data of every customer. In our
database all of this is stored in only one table, so that table is specified in the FROM clause.
Since we want to retrieve all columns from this table, instead of naming each of them
individually, we can use the abbreviation symbol * to indicate that all columns are to be
retrieved. That completes the recipe for our SQL statement which is shown below; note, we have
no use for the two optional clauses in this initial statement. In the same figure below, you will
also find the result of this query executed on a tiny database.

SELECT * FROM customers;


Customers
Tom Jewett 714-555-1212 10200 Slater 92708
Alvaro Monge 562-333-4141 2145 Main 90840
Wayne Dick 562-777-3030 1250 Bellflower 90840

SQL statement to retrieve all customers and the result set While the result of a query is known as
a result set, the result is not in fact always a set. The result could be a multi set, that is, a
collection of rows that can have duplicate rows.

Clearly we need to a refinement step as the query retrieves all customers while we are only
interested in customers who live in zip code 90840. We need to specify in the statement that the
only rows to retrieve from the database are those that meet this criteria. Such qualifying criteria
is specified in the WHERE clause using boolean expressions. Our first statement is thus refined
as shown in the figure below.

SELECT * FROM customers WHERE cZipCode = '90840';


Customers in zip code 90840
Alvaro Monge 562-333-4141 2145 Main 90840
Wayne Dick 562-777-3030 1250 Bellflower 90840

Refinement #2 to retrieve desired customers.

Note that SQL syntax requires the use of single quotes around literal strings like '90840'. While
not illustrated in this example and unlike SQL keywords, literal strings and strings stored in the
database are case sensitive; thus, 'Long Beach' is a different string than 'long beach'.
We need just a couple of more refinements. While we now are retrieving only the customers we
desire, we are also retrieving every column from the table yet, not all are needed. We need a way

Using Basic Structured Query Language Compiled by Bantegizia.Z


to pick the attributes (columns) we want. This is done by listing them in the SELECT clause,
each column name separated by a comma. The figure below shows this refinement and its
corresponding result set.
SELECT cLastName, cFirstName, cPhone FROM customers WHERE cZipCode = '90840';

Columns from SELECT


Monge Alvaro 562-333-4141
Dick Wayne 562-777-3030

1.4 Retrieving data from a specific column in a single table


Retrieve specific columns.Note that changing the order of the columns (like showing the last
name first) does not change the meaning of the results.
For practical purposes our last refinement is all that we need. To make the result set more
appealing to a human, we may want to order the result set. Imagine having a result set that is 100
times of what we are showing here! It would be better to display the result sorted alphabetically
by the name of the customer. In SQL, you can use the ORDER BY clause to specify the order in
which to retrieve the results. Once again, this ordering does not change the meaning of the
results; the result set does not change, all it changes is the order in which the rows are displayed.
This final refinement and its result are shown below.
SELECT cLastName, cFirstName, cPhone
FROM customers
WHERE cZipCode = '90840'
ORDER BY cLastName ASC, cFirstName ASC;
Rows in order

Dick Wayne 562-777-3030

Monge Alvaro 562-333-4141

1.4 Using clause to sort query output


SQL - SORTING Results
The SQL ORDER BY clause is used to sort the data in ascending or descending order, based on
one or more columns. Some databases sort the query results in an ascending order by default.

In addition to that, ORDER BY clause can also sort the data in a database table in a preferred
order. This case may not sort the records of a table in any standard order (like alphabetical or
lexicographical), but, they could be sorted based on any external condition. For instance, in an
ORDERS table containing the list of orders made by various customers of an organization, the
details of orders placed can be sorted based on the dates those orders are made. This need not be
alphabetically sorted,instead it is based on first come first serve.

Sorting Results in Ascending Order

Using Basic Structured Query Language Compiled by Bantegizia.Z


Using Order By Clause in SQL, the records in a database table can be sorted in ascending order,
either by default or by specifying the "ASC" keyword in the clause condition. Let us see an
example to understand this.
Example
Consider the CUSTOMERS table having the following records −
Following is an example, which would sort the result in an ascending order by NAME and
SALARY.
SELECT * FROM CUSTOMERS ORDER BY NAME;

1.5Retrieving restricted rows by placing a specific criteria in the clause


What are the Types of Clauses in SQL?

There are various types of clauses available in SQL, and some of them are listed below:

Clause Description
HAVING HAVING clause can be used in a GROUP BY clause. It is used to specify a search
condition for a group in the database tables.
WHERE The WHERE clause in SQL is used to retrieve the specific data from the database
that specifies the conditions exactly that are given in the UPDATE, DELETE, etc.
statements.
ORDER The ORDER BY clause in SQL is used for sorting the records of the database
BY tables.
GROUP To group the result set of the rows that have the same values in the result set from
BY the database tables, the GROUP BY clause is used.
TOP This clause is used when the database has many records. It is used to specify the
total number of records to be fetched or returned.
WITH WITH clause acts as a temporary view as it is available only during the execution
of SELECT, UPDATE, INSERT, DELETE, etc. statements. It is used to simplify
complex and long queries.
LIKE The SQL LIKE clause compares a value to similar values using wildcard operators,
i.e. per cent sign ( % ) and the underscore operator ( _ ).
FROM The FROM clause in SQL is used to select the database tables, which are
manipulated using the SELECT, DELETE, and UPDATE statements.
LIMIT The LIMIT clause is used when you are dealing with large databases. It is used to
specify the maximum number of rows to be retrieved from the table.
AND The AND clause is used when multiple conditions are specified in a query and
returns a dataset when all the conditions given in the AND clause meet the
requirements.
OR The OR clause is used when multiple conditions are specified in a query and returns
a dataset when one of those conditions gets satisfied.

What are the Uses of SQL Clause?


There are various uses of clauses in SQL based on the type of clause. The uses of some of the
clauses in SQL are mentioned below:

Using Basic Structured Query Language Compiled by Bantegizia.Z


ORDER BY
To sort the database records, you can use the ORDER BY clause in SQL. This clause in SQL is
used to arrange the fetched data in ascending or descending order based on the requirements.

WHERE
The WHERE clause in SQL is used to fetch the data or certain records that match the specified
condition in the SELECT statement. SQL's WHERE clause is also used with
the DELETE, UPDATE, etc. statements.
GROUP BY
Another usage of the clause in SQL is to group the rows that have the same values in the result
set, and this can be achieved by using the GROUP BY clause in SQL.
TOP
If you want to determine the total number of record rows in the result then you can use
the TOP clause in SQL.
AND
The AND clause is used with the UPDATE and DELETE statements and returns the resultant
dataset only when all the conditions given with the AND clause are satisfied.
OR
The OR clause is also used with the UPDATE and DELETE statements and returns the resultant
dataset when one or more than one condition is satisfied.
LIMIT
When the amount of data in the database is very large, the LIMIT clause is used to restrict the
number of rows from the database records.

Examples of Clause in SQL


Consider the below Students table, which is used as a reference for all the examples that are
mentioned below.
Students
stu_id stu_name stu_fees stu_subject stu_age stu_class
1 Divyesha Patil 3000 Maths 16 10
2 Mayra Pandit 2000 Social Science 15 10
3 Kunal Purohit 4500 Chemistry 17 11
4 Manvi Tyagi 2000 Social Science 16 9
5 Joy Yadav 3000 Maths 16 9
6 Tisha Shah 2500 Science 15 9
7 Surbhi Soni 4000 Chemistry 17 10
Example:
In this example, the below SQL query is used along with the WHERE clause in SQL to retrieve
all the records of a student from the Students table whose fees is less than 3500.
SELECT * FROM Students WHERE stu_fees < 3500;
Output:
stu_id stu_name stu_fees stu_subject stu_age stu_class
1 Divyesha Patil 3000 Maths 16 10
2 Mayra Pandit 2000 Social Science 15 10

Using Basic Structured Query Language Compiled by Bantegizia.Z


4 Manvi Tyagi 2000 Social Science 16 9
5 Joy Yadav 3000 Maths 16 9
6 Tisha Shah 2500 Science 15 9
As shown in the output, the WHERE clause in SQL fetches the records of those students whose
fee is less than 3500.

Example: The following query uses the GROUP BY clause to fetch the total fees in the students'
individual classes. This can be easily done by grouping of the rows from the Students table.
SELECT SUM(stu_fees), stu_class FROM Students GROUP BY stu_class;
Output:
stu_fees stu_class
9000 10
4500 11
7500 9
As shown in the output above, the GROUP BY clause is used to group the rows of the students
based on the student class column. The total fees in an individual class are summed up, and the
grouped rows are displayed in the table.

Example: Let's take another example which includes the ORDER BY clause in SQL. The below
query is used to order the students based on the fees of the individual students.
SELECT * FROM Students ORDER BY stu_fees;
Output:
stu_id stu_name stu_fees stu_subject stu_age stu_class
2 Mayra Pandit 2000 Social Science 16 10
4 Manvi Tyagi 2000 Social Science 16 9
6 Tisha Shah 2500 Science 15 9
1 Divyesha Patil 3000 Maths 16 10
5 Joy Yadav 3000 Maths 16 9
7 Surbhi Soni 4000 Chemistry 17 10
3 Kunal Purohit 4500 Chemistry 17 11
In the above example, the ORDER BY clause is applied to the column stu_fees to sort the final
result based on the fees of the students.

Example:
Consider another example which explains the HAVING clause in SQL. The following query
returns the details of all the students having an age less than 17 after grouping the records based
on stu_id.
SELECT * FROM Students GROUP BY stu_id HAVING stu_age < 17;
Output:
stu_id stu_name stu_fees stu_subject stu_age stu_class
1 Divyesha Patil 3000 Maths 16 10
2 Mayra Pandit 2000 Social Science 15 9
4 Manvi Tyagi 2000 Social Science 16 9
5 Joy Yadav 3000 Maths 16 10
6 Tisha Shah 2500 Science 15 9

Using Basic Structured Query Language Compiled by Bantegizia.Z


In the above output, you can see that the HAVING clause is used to fetch the records of students
under 17. Also, note that the GROUP BY clause is mandatory if you are using
the HAVING clause in SQL.

1.7 Retrieving restricted rows by placing specific criteria in select statement


1.8 Using comparison operators in the clause to compare numeric, character,
string, date and time data
Comparison operator
A comparison (or relational) operator is a mathematical symbol which is used to compare two
values. Comparison operators are used in conditions that compare one expression with another.
The result of a comparison can be TRUE, FALSE, or UNKNOWN (an operator that has one or
two NULL expressions returns UNKNOWN).

The following table describes different types of comparison operators -


Operator Description Operates on
= Equal to. Any compatible data types
> Greater than. Any compatible data types
< Less than. Any compatible data types
>= Greater than equal to. Any compatible data types
<= Less than equal to. Any compatible data types
<> Not equal to. Any compatible data types

1.9 Using Boolean operators with correct precedence


1.10 Using criteria in the where clause in
1.10.1 Checking for a range of values
The SQL BETWEEN Operator
The BETWEEN operator selects values within a given range. The values can be numbers, text,
or dates.
The BETWEEN operator is inclusive: begin and end values are included.
SELECT productName, unitPrice FROM products WHERE unitPrice
BETWEEN 18 AND 19;
SELECT * FROM Products WHERE Price NOT BETWEEN 10 AND 20;

1.10.2 Selecting values from a list


A) Using SQL Server IN with a list of values example
The following statement finds the products whose list price is one of the following values: 89.99,
109.99, and 159.99:

SELECT product_name, list_price FROM production.products


WHERE list_price IN (89.99, 109.99, 159.99) ORDER BY list_price;

1.10.3checking for values that match a pattern


The SQL LIKE Operator for Pattern Matching

Using Basic Structured Query Language Compiled by Bantegizia.Z


Like it or not, the LIKE operator is essential in SQL. It gives data practitioners the power to filter
data on specific string matches.

SELECT DISTINCT first_name FROM employees WHERE first_name LIKE 'A%'

1.11 Using SQL syntax to supress duplicate values from query result
How to Remove Duplicates in SQL Using the DISTINCT Keyword
One of the easiest ways to remove duplicate data in SQL is by using the DISTINCT keyword.
You can use the DISTINCT keyword in a SELECT statement to retrieve only unique values
from a particular column.

Here's an example of how to use the DISTINCT keyword to remove duplicates from a table:
SELECT DISTINCT customer_name FROM customers;

How to Remove Duplicates in SQL Using the GROUP BY Clause


Another way to remove duplicates in SQL is by using the GROUP BY clause. The GROUP BY
clause groups rows based on the values in a specific column and returns only one row for each
unique value.

Here's an example of how to use the GROUP BY clause to remove duplicates from a table:
SELECT customer_id FROM orders GROUP BY customer_id;

How to Remove Duplicates in SQL Using the INNER JOIN Statement


Another way to remove duplicates in SQL is by using the INNER JOIN statement. The INNER
JOIN statement combines rows from two or more tables based on a related column between
them. By joining a table with itself, we can compare rows and remove duplicates.

Here's an example of how to use the INNER JOIN statement to remove duplicates from a table:
SELECT a.column_name
FROM table_name a
INNER JOIN table_name b ON a.column_name = b.column_name
WHERE a.primary_key > b.primary_key;

For example, if we have a table called "employees" with columns "employee_id",


"employee_name", and "department_id", we can use the following SQL query to remove
duplicates from the "department_id" column:

SELECT a.department_id
FROM employees a
INNER JOIN employees b ON a.department_id = b.department_id
WHERE a.employee_id > b.employee_id;

1.12 Taking action to execute null values from a query result


What is a SQL NULL value?

Using Basic Structured Query Language Compiled by Bantegizia.Z


In terms of the relational database model, a NULL value indicates an unknown value. If we
widen this theoretical explanation, the NULL value points to an unknown value but this
unknown value does not equivalent to a zero value or a field that contains spaces. Due to this
structure of the NULL values, it is not possible to use traditional comparison (=, <, > and <>)
operators in the queries. As a matter of fact, in the SQL Standards using the WHERE clause as
the below will lead to return empty result sets.

IS NULL Condition
The IS NULL condition is used to return rows that contain the NULL values in a column and its
syntax is like below:
The following query will retrieve the rows from the Person table which are MiddleName column
values are equal to NULL.

SELECT FirstName, LastName ,MiddleName FROM Person.Person WHERE


MiddleName IS NULL

LO2. Write SQL statements that use functions


2.1. Using arithmetical operators with the correct precedence
Arithmetic Operators
You can use arithmetic operators in Multidimensional Expressions (MDX) for any arithmetic
computations, including addition, subtraction, multiplication, and division.

MDX supports the arithmetic operators listed in the following table.


Operator Description
+ (Add) Adds two numbers.
/ (Divide) Divides one number by another number.
* (Multiply) Multiplies two numbers.
- (Subtract) Subtracts two numbers.
^ (Power) Raises one number by another number.

Order of Precedence
The following rules determine the order of precedence for arithmetic operators in an MDX
expression:
 When there is more than one arithmetic operator in an expression, MDX performs
multiplication and division first, followed by subtraction and addition.
 When all arithmetic operators in an expression have the same level of precedence, the
order of execution is left to right.
 Expressions within parentheses take precedence over all other operations.
SELECT 100 + 200 ;
SELECT 45 - 74 ;
SELECT 25 * 4 ;
SELECT 36 / 6 ;
SELECT 17 % 4 ;

Using Basic Structured Query Language Compiled by Bantegizia.Z


The below SQL Server example calculates the employee’s salary after the addition of 10%.
SELECT ID, Name, Department, Salary,
Salary * 1.1 AS CalulatedSalary
FROM Employee
It is always advisable to specify the original column without any change (salary) in addition to
the column with the calculation (salary *1.1) in order to make the difference. It is also possible to
perform any mathematical calculation as shown in the below example.
SELECT ID, Name, Department, Salary,
Salary * 0.11 / 2.54 + 27.36 AS CalulatedSalary
FROM Employee

2.2. Using string functions and operators to obtain required query output
SQL string functions are used primarily for string manipulation. The following table details the
important string functions –

Example
The following SELECT query displays the SQL ASCII value of the first character of the given
string.
SELECT ASCII ("SQL stands for Structured Query Language") AS ASCII_S;
The following SELECT query displays the ASCII value of the string we have given.
select ASCII ("Kaushik");
The CHAR() function does not support multiple integer arguments, if we try to pass multiple
values it generates an error saying "The char function requires 1 argument(s)"
We can also use this function with along with the table columns, by passing them as parameters,
along with strings, and characters.

Example
The following SELECT query displays the CHAR value of 100 −
SELECT CHAR(100) AS char_function;
The SQL CHARINDEX() function searches for a substring within a string starting at the
specified location and returns the position of the substring found. If the substring was not found
this function returns 0. It accepts three parameters, substring, string(The string to be searched),
start(It is an optional parameter) The position where the search will start.
Parameters
Here, CHARINDEX() Function accepts three parameters −
 substring − The substring to search for, It has the limit of 8000 characters.
 string − The string to be searched.
 start − It is an optional parameter. The position where the search will start (if you do not
want to start at the beginning of string). The first position in string is 1.
Return value
This function returns the position of a substring within the given string, If the substring is not
found in the string, the function returns 0.

Example

Using Basic Structured Query Language Compiled by Bantegizia.Z


Following is an example to Search a character using the CHARINDEX() function.
SELECT CHARINDEX('s', 'Tutorialspoint') As charindex;

The SQL CONCAT_WS()


The SQL CONCAT_WS() function accepts a separator, one or more string values as parameters
and, concatenates/join all the given strings placing the specified separator in between them and
returns the result.
The first argument of this function is considered as the separator. It can be a string or a numerical
value. If the separator value is NULL this function returns NULL.
If there are any empty stings in the string values they will be included in the result. But if you
pass NULL as a string value (after the separator) it is neglected.
Parameters
str − It accepts the string(one or more string) along with the separator.
Return value
The SQL CONCAT_WS() function returns the same string as the operator returns from the same
expressions as the operands along with separator.

Example
The following SELECT query adds two characters and creates a new string −
SELECT CONCAT_WS('_','Tutorialspoint', 'Company') AS CONCAT_WSfunction;
The SQL CONCAT() function accepts a one or more string values as parameters,
concatenates/join all the given strings and returns the result.

When we display the result, the Concat service converts the Null values to an empty string. The
operator is used to concatenate character strings and column strings. In the CONCAT function,
we can use a literal. A literal is a number, character, or date that is contained in a SELECT
statement.

The SQL CONCAT() function accepts a one or more string values as parameters,
concatenates/join all the given strings and returns the result.
When we display the result, the Concat service converts the Null values to an empty string. The
operator is used to concatenate character strings and column strings. In the CONCAT function,
We can use a literal. A literal is a number, character, or date that is contained in a SELECT
statement.

The SQL server DIFFERENCE() function is used to compare two SOUNDEX values of the
strings. It accepts two parameters, exp1 and exp2 and returns an integer value that indicates a
match for two SOUNDEX values, from 0 to 4.
The Soundex value is a four-character code that is based on how the string sounds when spoken
in English. Here, the 0 value indicates little or no similarity between SOUNDEX values, whereas
the value 4 indicates strong similarity or matching SOUNDEX values.

Parameters

Using Basic Structured Query Language Compiled by Bantegizia.Z


This method accepts two parameters both are alphanumeric representation of character data,
which can be a constant, variable or column.
Return valueIt returns an integer value measuring the difference between the SOUNDEX()
values of two different expressions(strings).

Example
Following is an example using DIFFERENCE() function with similar SOUNDEX values −
SELECT SOUNDEX('Had') AS soundex_Had, SOUNDEX('Hadi') AS soundex_Hadi,
DIFFERENCE('Had', 'Hadi') AS similarity;

2.3. Using mathematical functions to obtain required query output


Mathematical Functions in SQL
Mathematical functions are very important in SQL to implement different mathematical concepts
in queries.
Some of the the major mathematical functions in SQL are as follows −
ABS(X)
This function returns the absolute value of X. For example −
Select abs(-6);
This returns 6.

MOD(X,Y)
The variable X is divided by Y and their remainder is returned. For example −
Select mod(9,5);
This returns 4.

SIGN(X)
This method returns 1 if X is positive, -1 if it is negative and 0 if the value of X is 0. For example

Select sign(10);
This returns 1.

FLOOR(X)
This returns the largest integer value that is either less than X or equal to it. For example −
Select floor(5.7);
This returns 5.

CEIL(X)
This returns the smallest integer value that is either more than X or equal to it. For example −
Select ceil(5.7);
This returns 6.

POWER(X,Y)
This function returns the value of x raised to the power of Y For example −
Select power(2,5);
This returns 32.

Using Basic Structured Query Language Compiled by Bantegizia.Z


ROUND(X)
This function returns the value of X rounded off to the whole integer that is nearest to it. For
example −
Select round(5.7);
This returns 6.

SQRT(X)
This function returns the square root of X. For example −
Select sqrt(9);
This returns 3.

ASIN(X)
This function accepts a Sin value as the input and returns the angle in radians. For example −
Select asin(0);
This returns 0.

ACOS(X)
This function accepts a Cos value as the input and returns the angle in radians. For example −
Select acos(1);
This returns 0.

ATAN(X)
This function accepts a Tan value as the input and returns the angle in radians. For example −
Select atan(0);
This returns 0.

SIN(X)
This function accepts an angle in radians as its parameter and returns its Sine value. For example

Select sin(0);
This returns 0.

COS(X)
This function accepts an angle in radians as its parameter and returns its Cosine value. For
example −
Select cos(0);
This returns 1.

TAN(X)
This function accepts an angle in radians as its parameter and returns its Tan value. For example

Select tan(0);
This returns 0.

2.4. Using date functions to obtain required query output

Using Basic Structured Query Language Compiled by Bantegizia.Z


In general, time is represented using three values: hours, minutes, and seconds. We can store
time in various formats.
 HH:MM:SS (Hours:Minutes:Second)
 It stores and displays time in the day format (AM/PM), for example, 10:00 AM/10:00
PM.
 It (Time) also stores and displays time in 24-hour format; a 24-hour clock runs from
00:00 (midnight) to 23:59.
Date is also represented using three values: date, month, and year. Dates have many possible
variations, all of which depend on several inconsistency factors.
 DD/MM/YYYY, For example - 06/02/2023
 MM/DD/YYYY, For example - 02/06/2023
 DD-MM-YYYY, For example - 02-06-2023

Date and Time in SQL


The date and time functions in SQL are used to effectively handle date and time data. While
working with a database, the format of the date and time functions should be matched while
inserting data into the table.
There are several different date and time functions available in SQL Server. The purpose of
include them is to ensure that the date and time module is usable while creating and using
databases.

Formats of date and time in SQL


SQL server by default stores date, smalldatetime, timestamp and year values in a certain format.
Following are the formats of date and time used in the SQL server −
 DATE - YYYY-MM-DD
 DATETIME - YYYY-MM-DD HH:MI:SS
 SMALLDATETIME - YYYY-MM-DD HH:MI:SS
 TIMESTAMP - a unique number
Inserting date and time values in a table
To insert date and time values in a table, follow the steps below −
 First, you must create a table that accepts date and time values.
 Second, you must insert the data into the newly created table, which accepts date and
time data types.
CREATE TABLE customers_details(orderDate DATE, shippingDate DATETIME,
deliveredDate TIMESTAMP, time TIME);
Verification
After the table has been created, we could use this query to check its details "EXEC sp_help
'dbo.customers_details'" in the SQL.
NSERT INTO customers_details VALUES('2023-02-01', '2023-02-01 :10:00','2023-02-03
:18:00', '18:00');
Operation on date & time
In this event, we're performing the function and displaying its result, which contains the date and
time.

GETDATE() function

Using Basic Structured Query Language Compiled by Bantegizia.Z


The GETDATE() function returns datetime datatypes and is typically used to obtain the current
date, as we can see in the following SQL query. GETDATE() it returns the current date with
current time.
SELECT GETDATE() AS 'current datetime';

Verification
When we execute the above SQL query, we get the current date with time as follow −
+-------------------------+
| current datetime |
+-------------------------+
| 2023-02-06 14:10:49.860 |
+-------------------------+

CURRENT_TIMESTAMP
The current_timestamp is used to obtain the current timestamp and it returns the same date and
time as the GETDATE() function. Its datatypes are also date and time. As shown in the
following SQL query, the CURRENT_TIMESTAMP typically returns the current time along
with the current date −
SELECT CURRENT_TIMESTAMP AS 'CURRENTTIMESTAMP';

SYSDATETIME() Function
The SYSDATETIME() function is also used to obtain the current time of the system on which
the SQL server instance is running. It has larger fractional-second precision compared to the
GETDATE() function. Following is the SQL query for the SYSDATETIME() function.
SELECT SYSDATETIME() AS 'Current Time and Date';

CONVERT() Function
We are extracting the time using the GETDATE() OR CURRENT_TIMESTAMP inside convert
function to separate the time component from the current date (from the SYSDATE), as we can
see in the SQL query that follow −
SELECT CONVERT(VARCHAR(8), GETDATE(), 108) AS 'HH:MM:SS';

Verification
When we run the above SQL query, we only get the time in "HH:MM:SS" because we are using
108. 108 is a time-only format that displays the time in the "HH:MM:SS" format.

2.5. Using SQL aggregate functions to obtain required query output


SQL Server Aggregate Functions
Aggregate functions in SQL Server are used to perform calculations on one or more values and
return the result in a single value. In SQL Server, all aggregate functions are built-in functions
that avoid NULL values except for COUNT(*). We mainly use these functions with the GROUP
BY and HAVING clauses of the SELECT statements in the database query languages.

DBA generally used these functions for summarizing their data. When aggregate functions are
invoked with a particular set of input values multiple times, they always return the same value.

Using Basic Structured Query Language Compiled by Bantegizia.Z


Therefore, they are also called deterministic functions. It is noted that the aggregate functions
cannot be nested, and the expression cannot be a subquery.
Syntax:
The following are the syntax to use aggregate functions in MySQL:
aggregate_function_name(DISTINCT | ALL exp)
In this syntax, we can see the following parameters:
aggregate_function_name: It indicates the name of the aggregate function that we want to use.
DISTINCT | ALL: The DISTINCT modifier is used when we want to consider the distinct
values in the calculation. The ALL modifiers are used when we want to calculate all values,
including duplicates. If we do specify any modifier, all aggregate functions use the ALL modifier
by default.
exp: It indicates the table's columns or an expression containing multiple columns with
arithmetic operators.
SQL Server provides various aggregate functions, and the most commonly used aggregate

functions are shown in the below table:


Aggregate Descriptions
Function
COUNT() This function counts the number of elements or rows, including NULL values in
the defined set.
SUM() This function calculates the total sum of all NON-NULL values in the given set.
AVG() This function performs a calculation on NON-NULL values to get the average
of them in a defined set.
MIN() This function returns the minimum (lowest) value in a set.
MAX() This function returns the maximum (highest) value in a set.

This table shows some other aggregate functions used in SQL Server:
Aggregate Function Descriptions
CHECKSUM_AGG It calculates the checksum of the values in a defined set.
COUNT_BIG() It counts the number of elements, including NULL values in a defined set.
This function is the same as the COUNT() function, but it returns a BIG INT
data type, whereas COUNT returns an INT data type.
STDEV() It calculates the statistical standard deviation of each value in the defined
expression on the basis of a sample data population.
STDEVP() It calculates the standard deviation for each value in the given expression on
the basis of an entire data population.
VAR() It calculates the statistical variance of each element in the defined expression
on the basis of a sample data population.
VARP() It calculates the statistical variance of each element in the defined expression
on the basis of an entire data population.
GROUPING() It signifies whether or not a GROUP BY lists specified column expression is
aggregated. If the result set shows 1, it means the result set is aggregated and,
if not, returns 0.
GROUPING_ID() It is used to computes the level of grouping.
Why we use aggregate functions?

Using Basic Structured Query Language Compiled by Bantegizia.Z


The aggregate functions are mainly used to produce the summarized data in economics and
finance to represent the economic health or stock and sector performance. In the context of
business, different organization levels need different information, such as top levels managers
interested in knowing whole figures and not the individual details.

Aggregate Functions Example


Let us understand how the most commonly used aggregate functions work on the database. Here
we will first create an employee table for the demonstration of all aggregate functions.
Execute the below statement to create an employee table:

CREATE TABLE employee(


name varchar(45) NOT NULL,
occupation varchar(35) NOT NULL,
working_date date,
working_hours varchar(10),
salary INT
);

Next, we will insert some data into this table as follows:

INSERT INTO employee VALUES


('Jolly Evans', 'HR', '2020-10-04', 9, 25000),
('Brayden Simmons', 'Engineer', '2020-10-04', 12, 65000),
('Rose Huges', 'Writer', '2020-10-04', 13, 35000),
('Laura Paul', 'Manager', '2020-10-04', 10, 45000),
('Diego Simmons', 'Teacher', '2020-10-04', 12, 30000),
('Antonio Bennet', 'Writer', '2020-10-04', 13, 35000);

We can see the table records using the SELECT statement:

COUNT() Function
This function returns the total number of rows, including NULL values in the given expression.
It can also count all records based on a specified condition and returns zero if it does not find
any matching records. It can work with both numeric and non-numeric data types.

Example
The below example uses the COUNT() function and returns the total number of employees data
stored in the employee table:

Using Basic Structured Query Language Compiled by Bantegizia.Z


SELECT COUNT(*) AS total_employees FROM employee;
Output:

To read more information, click here.

SUM() Function
This function calculates the total summation of NON-NULL values in the given set. It returns
NULL if the result set does not have any records. The SUM function can only work with
the numeric data type.

Example
The below example uses the SUM function and calculates the total summed up salary of all
employees stored in the employee table:
SELECT SUM(salary) AS total_salary FROM employee;
Output:
After execution, we can see the total salary of all employees in the table:

AVG() Function
This function calculates the average of NON-NULL values specified in the column. The AVG
function can only work with the numeric data type.
Example
The below example uses the AVG function and calculates the average salary of employees stored
in the employee table:
SELECT AVG(salary) AS "Average Salary" FROM employee;

Output:

Using Basic Structured Query Language Compiled by Bantegizia.Z


MIN() Function
This function gives the minimum (lowest) value of the specified column. It also works with
numeric data types only.

Example
The below example uses the MIN function and returns the lowest salary of an employee stored in
the employee table:
SELECT MIN(salary) AS "Lowest Salary" FROM employee;
Output:
Here we can see that the minimum salary of an employee available in the table:

MAX() Function
This function gives the maximum (highest) value of the specified column. It also works with
numeric data types only.

Example
The below example uses the MAX function and returns the highest salary of employees stored in
the employee table:
SELECT MAX(salary) AS "Highest Salary" FROM employee;

Output:

Aggregate Functions & JOINs

Using Basic Structured Query Language Compiled by Bantegizia.Z


SQL Server can enable us to use the aggregate function to retrieve resultant data from more than
one table. To understand this concept, we will create another table named "emp_address" that
stores the address of each employee. Here is the query to create a table:

CREATE TABLE emp_address(


name varchar(45),
cellphone varchar(25),
address varchar(90),
city varchar(35)
);

INSERT INTO emp_address(name, cellphone, address, city) VALUES


('Jolly Evans', '334369253659', '777 Brockton Avenue, Abington MA 2351', 'California'),
('Brayden Simmons', '359436598356', '3849 Route 31, Clay NY 12041', 'New York'),
('Rose Huges', '768055634859', '345 East Meighan Blvd, Gadsden AL 359', 'Alaska'),
('Laura Paul', '948563945327', '301 RT 9W, Glenmont NY 12077', 'New York'),
('Diego Simmons', '465676423435', '501 Memorial Dr, Chicopee MA 1030', 'California'),
('Antonio Bennet', '506705670323', '317 Russell St, Hadley MA 1415', 'California');

Suppose we want to calculate the total number of employee and their addresses from two
different tables. We can do this by using the below statement:

SELECT COUNT(employee.name) AS Names, COUNT(emp_address.city) AS addresses FRO


M employee INNER JOIN emp_address ON employee.name = emp_address.name;

LO3. Write SQL statements that use aggregation and filtering

Using Basic Structured Query Language Compiled by Bantegizia.Z


3.1 Using clause to aggregate data by multiple columns
HAVING Clause in SQL
The HAVING clause places the condition in the groups defined by the GROUP BY clause in the
SELECT statement. This SQL clause is implemented after the 'GROUP BY' clause in the
'SELECT' statement. This clause is used in SQL because we cannot use the WHERE clause with
the SQL aggregate functions. Both WHERE and HAVING clauses are used for filtering the
records in SQL queries.

Difference between HAVING and WHERE Clause


The difference between the WHERE and HAVING clauses in the database is the most important
question asked during an IT interview.
The following table shows the comparisons between these two clauses, but the main difference is
that the WHERE clause uses condition for filtering records before any groupings are made, while

HAVING clause uses condition for filtering values from a group.


HAVING WHERE
1. The HAVING clause is used in database 1. The WHERE clause is used in database systems
systems to fetch the data/values from the groups to fetch the data/values from the tables according
according to the given condition. to the given condition.
2. The HAVING clause is always executed with 2. The WHERE clause can be executed without
the GROUP BY clause. the GROUP BY clause.
3. The HAVING clause can include SQL 3. We cannot use the SQL aggregate function with
aggregate functions in a query or statement. WHERE clause in statements.
4. We can only use SELECT statement with 4. Whereas, we can easily use WHERE clause
HAVING clause for filtering the records. with UPDATE, DELETE, and SELECT
statements.
5. The HAVING clause is used in SQL queries 5. The WHERE clause is always used before the
after the GROUP BY clause. GROUP BY clause in SQL queries.
6. We can implements this SQL clause in column 6. We can implements this SQL clause in row
operations. operations.
7. It is a post-filter. 7. It is a pre-filter.
8. It is used to filter groups. 8. It is used to filter the single record of the table.

Syntax of HAVING clause in SQL


SELECT column_Name1, column_Name2, ....., column_NameN aggregate_function_name(colu
mn_Name) FROM table_name GROUP BY column_Name1 HAVING condition;

Examples of HAVING clause in SQL


In this article, we have taken the following four different examples which will help you how to
use the HAVING clause with different SQL aggregate functions:

Example 1: Let's take the following Employee table, which helps you to analyze the HAVING
clause with SUM aggregate function:
Emp_Id Emp_Name Emp_Salary Emp_City
201 Abhay 2000 Goa

Using Basic Structured Query Language Compiled by Bantegizia.Z


202 Ankit 4000 Delhi
203 Bheem 8000 Jaipur
204 Ram 2000 Goa
205 Sumit 5000 Delhi

If you want to add the salary of employees for each city, you have to write the following query:
SELECT SUM(Emp_Salary), Emp_City FROM Employee GROUP BY Emp_City;

The output of the above query shows the following output:


SUM(Emp_Salary) Emp_City
4000 Goa
9000 Delhi
8000 Jaipur

Now, suppose that you want to show those cities whose total salary of employees is more than
5000. For this case, you have to type the following query with the HAVING clause in SQL:
SELECT SUM(Emp_Salary), Emp_City FROM Employee GROUP BY Emp_City HAVING S
UM(Emp_Salary)>5000;

The output of the above SQL query shows the following table in the output:
SUM(Emp_Salary) Emp_City
9000 Delhi
8000 Jaipur

Example 2: Let's take the following Student_details table, which helps you to analyze the
HAVING clause with the COUNT aggregate function:
Roll_No Name Marks Age
1 Rithik 91 20
2 Kapil 60 19
3 Arun 82 17
4 Ram 92 18
5 Anuj 50 20
6 Suman 88 18
7 Sheetal 57 19
8 Anuj 64 20
Suppose, you want to count the number of students from the above table according to their age.
For this, you have to write the following query:
SELECT COUNT(Roll_No), Age FROM Student_details GROUP BY Age ;

The above query will show the following output:


Count(Roll_No) Age
3 20
2 19
1 17

Using Basic Structured Query Language Compiled by Bantegizia.Z


2 18

Now, suppose that you want to show the age of those students whose roll number is more than
and equals 2. For this case, you have to type the following query with the HAVING clause in
SQL:
SELECT COUNT(Roll_No), Age FROM Student_details GROUP BY Age HAVING COUNT(
Roll_No) >= 2 ;

The output of the above SQL query shows the following table in the output:
Count(Roll_No) Age
3 20
2 19
2 18

Example 3: Let's take the following Employee table, which helps you to analyze the HAVING
clause with MIN and MAX aggregate function:
Emp_ID Name Emp_Salary Emp_Dept
1001 Anuj 9000 Finance
1002 Saket 4000 HR
1003 Raman 3000 Coding
1004 Renu 6000 Coding
1005 Seenu 5000 HR
1006 Mohan 10000 Marketing
1007 Anaya 4000 Coding
1008 Parul 8000 Finance

MIN Function with HAVING Clause:


If you want to show each department and the minimum salary in each department, you have to
write the following query:
SELECT MIN(Emp_Salary), Emp_Dept FROM Employee GROUP BY Emp_Dept;

The output of the above query shows the following output:


MIN(Emp_Salary) Emp_Dept
8000 Finance
4000 HR
3000 Coding
10000 Marketing

Now, suppose that you want to show only those departments whose minimum salary of
employees is greater than 4000. For this case, you have to type the following query with the

HAVING clause in SQL:


SELECT MIN(Emp_Salary), Emp_Dept FROM Employee GROUP BY Emp_Dept HAVING M
IN(Emp_Salary) > 4000 ;

Using Basic Structured Query Language Compiled by Bantegizia.Z


The above SQL query shows the following table in the output:
MIN(Emp_Salary) Emp_Dept
8000 Finance
10000 Marketing

MAX Function with HAVING Clause:


In the above employee table, if you want to list each department and the maximum salary in each
department. For this, you have to write the following query:
SELECT MAX(Emp_Salary), Emp_Dept FROM Employee GROUP BY Emp_Dept;
The above query will show the following output:
MAX(Emp_Salary) Emp_Dept
9000 Finance
5000 HR
6000 Coding
10000 Marketing
Now, suppose that you want to show only those departments whose maximum salary of
employees is less than 8000. For this case, you have to type the following query with the

HAVING clause in SQL:


SELECT MAX(Emp_Salary), Emp_Dept FROM Employee GROUP BY Emp_Dept HAVING
MAX(Emp_Salary) < 8000 ;
The output of the above SQL query shows the following table in the output:
MAX(Emp_Salary) Emp_Dept
5000 HR
6000 Coding

Example 4: Let's take the following Employee_Dept table, which helps you to analyze the
HAVING clause with AVG aggregate function:
Emp_ID Name Emp_Salary Emp_Dept
1001 Anuj 8000 Finance
1002 Saket 4000 HR
1003 Raman 3000 Coding
1004 Renu 6000 Coding
1005 Seenu 5000 HR
1006 Mohan 10000 Marketing
1007 Anaya 4000 Coding
1008 Parul 6000 Finance

If you want to find the average salary of employees in each department, you have to write the
following query:
SELECT AVG(Emp_Salary), Emp_Dept FROM Employee_Dept GROUP BY Emp_Dept;
The above query will show the following output:
AVG(Emp_Salary) Emp_Dept
7000 Finance
4500 HR

Using Basic Structured Query Language Compiled by Bantegizia.Z


6500 Coding
10000 Marketing
Now, suppose that you want to show those departments whose average salary is more than and
equals 6500. For this case, you have to type the following query with the HAVING clause in
SQL:

SELECT AVG(Emp_Salary), Emp_Dept FROM Employee_Dept GROUP BY Emp_Dept HAVI


NG AVG(Emp_Salary) > 6500 ;

The above SQL query will show the following table in the output:
AVG(Emp_Salary) Emp_Dept
7000 Finance
6500 Coding
10000 Marketing

3.2 Sorting aggregate data in query output


How to Use GROUP BY in SQL
In this SQL section, you will learn what the GROUP BY keyword is and how to implement it in
Structured Query Language. We will also discuss how to use GROUP BY clause with WHERE
clause.

What is GROUP BY?


GROUP BY is an SQL keyword used in the SELECT query for arranging the same values of a
column in the group by using SQL functions.

Syntax of GROUP BY clause


SELECT Column_Name_1, Column_Name_2, ........, Column_Name_N FROM Table_Name G
ROUP BY Column_Name_1, Column_Name_2, ........, Column_Name_N;
We can use more than one field of the table in the GROUP BY clause. We have to separate the
name of multiple columns by a comma.
Follow the steps given below to learn how to use the GROUP BY clause in the SQL table:
 Create a Simple Database and Table.
 Insert Data into the Table
 View the Inserted Data without the GROUP BY clause.
 Use the GROUP BY clause.

Step 1: Create a simple Database and Table


First, you have to create a new database in SQL.The following query creates the Hospital
Database:
CREATE Database Hospital;

Now, you have to create the new table using the following CREATE TABLE syntax:
CREATE TABLE table_name
(
column_Name_1 data type (size of the column_1),
column_Name_2 data type (size of the column_2),

Using Basic Structured Query Language Compiled by Bantegizia.Z


column_Name_3 data type (size of the column_3),
...
column_Name_N data type (size of the column_1)
);

The following query creates the Doctor_Info table in the Hospital Database:

CREATE TABLE Doctor_Info


(
Doctor_ID Int PRIMARY KEY,
Doctor_Name VARCHAR (100),
Doctor_Specialist VARCHAR (80),
Doctor_GenderVarchar (20),
Doctor_Country Varchar (80)
);

Step 2: Insert the Data into the table


Now, you have to insert the data in the table using the following syntax:

INSERT INTO <Table_Name> (Column_Name_1, Column_Name_2, Column_Name_3, .......,


Column_Name_N) VALUES (value_1 of Column_1, value_2, value_3, ...., value_N);

The following query inserts the record of those doctors who work in Hospital:

INSERT INTO Doctor_Info (Doctor_ID, Doctor_Name, Doctor_Specialist, Doctor_Gender, Doc


tor_Country) VALUES ( 1035, Jones, Malaria_Specialist, Male, United Kingdom),
(1015, Marry, Diabties_Specialist, Female, United States),
(1003, Harry, Fever_Specialist, Male, United Kingdom),
(1044, Ella, Cancer_Specialist, Female, United States),
(1025, Moria, Corona_Specialist, Female, Europe);

Step 3: View the Inserted data of Table Without GROUP BY


The following query shows the record of Doctors in an unsorted manner:
SELECT * FROM Doctor_Info;

The output of the above SELECT query is shown below:


Doctor_ID Doctor_Name Doctor_Disease Doctor_Gender Doctor_Country
1035 Jones Malaria_Specialist Male United Kingdom
1015 Marry Diabities_Specialist Female United State
1003 Harry Fever_Specialist Male United Kingdom
1044 Ella Cancer_Specialist Female United State
1025 Moria Corona_Specialist Female Europe

Step 4: Use GROUP BY clause


The following SQL query uses the GROUP BY keyword to list the number of doctors in each
country:

Using Basic Structured Query Language Compiled by Bantegizia.Z


SELECT COUNT (Doctor_ID), Doctor_Country GROUP BY Doctor_Country;
The output of the above SELECT with ODER BY query is shown in the following Doctor table:

Output:

GROUP BY with SQL ORDER BY clause


We can also use the ORDER BY keyword with the GROUP BY clause in the SQL SELECT
statement.

Syntax of GROUP BY clause with ORDER BY clause

SELECT Function_Name (Column_Name) FROM Table_Name GROUP BY Column_Name O


RDER BY Function_Name (Column_Name);

Example of GROUP BY clause with ORDER BY clause


The following query creates the new Subject table in the School database:
CREATE TABLE Subject
(
Subject_ID INT PRIMARY KEY,
Subject_Name VARCHAR (50),
Subject_Teacher VARCHAR (70),
Student_ID INT
);
The following INSERT INTO query inserts the records into the Subject table:
INSERT INTO Subject(Subject_ID, Subject_Name, Subject_Teacher, Student_ID) VAL
UES (2211, Maths, Ramesh, 101),
(2252, English, Somya, 103),
(2201, Chemistry, Suresh, 101),
(2224, Physics, Aman, 103),
(2248, Computer, Bhanu, 101),
(2208, Hindi, Sonu, 104),
(2221, Biology, Punit, 104));
The following query uses the SQL ORDER BY clause with GROUP BY:
SELECT Count(Subject_ID), Student_ID FROM Subject GROUP BY Student_ID ORDER BY
COUNT(Subject_ID) DESC;

Output:

Using Basic Structured Query Language Compiled by Bantegizia.Z


GROUP BY clause with MIN function
We can also use the MIN aggregate function with the GROUP BY clause in Structured Query
Language.

Syntax of Group BY clause with MIN function:


SELECT Column_Name_1, MIN(Column_Name) FROM Table_Name GROUP BY Column_N
ame_1;

Example of MIN Aggregate Function with GROUP BY Clause


This example takes the below School_Stu_Details table to understand the concept of GROUP
BY clause with MIN aggregate function:
CREATE TABLE School_Stu_Details
(
Stu_ID INT NOT NULL,
Stu_Name varchar(100),
Stu_Subject varchar(50),
Stu_Age INT,
Stu_Marks INT
);
The following INSERT INTO statements insert the record of School students:
INSERT INTO School_Stu_Details VALUES (101, Anuj, English, 20, 88);
INSERT INTO School_Stu_Details VALUES (102, Raman, Maths, 24, 98);
INSERT INTO School_Stu_Details VALUES (104, Shyam, Hindi, 19, 92);
INSERT INTO School_Stu_Details VALUES (107, Vikash, Computer, 20, 78);
INSERT INTO School_Stu_Details VALUES (111, Monu, English, 21, 65);
INSERT INTO School_Stu_Details VALUES (114, Jones, Hindi, 18, 93);
INSERT INTO School_Stu_Details VALUES (121, Parul, Maths, 20, 97);
INSERT INTO School_Stu_Details VALUES (123, Divya, English, 21, 89);
INSERT INTO School_Stu_Details VALUES (128, Hemant, Computer, 23, 90);
INSERT INTO School_Stu_Details VALUES (130, Nidhi, Hindi, 20, 88);
INSERT INTO School_Stu_Details VALUES (132, Priya, English, 22, 99);
INSERT INTO School_Stu_Details VALUES (138, Mohit, Maths, 21, 92);
The following query simply shows the record of students in the tabular form:
SELECT * FROM School_Stu_Details;

Stu_ID Stu_Name Stu_Subject Stu_Age Stu_Marks


101 Anuj English 20 88
102 Raman Maths 24 98

Using Basic Structured Query Language Compiled by Bantegizia.Z


104 Shyam Hindi 19 92
107 Vikash Computer 20 78
111 Monu English 21 65
114 Jones Hindi 18 93
121 Parul Maths 20 97
123 Divya English 21 89
128 Hemant Computer 23 90
130 Nidhi Hindi 20 88
132 Priya English 22 99
138 Mohit Maths 21 92

The following query shows the minimum marks of a student in each subject from the above
School_Stu_Details table:

SELECT Stu_Subject, MIN (Stu_Marks) FROM School_Stu_Details GROUP BY Stu_Subject;


Output:
Stu_Subject MIN (Stu Marks)
English 65
Maths 92
Hindi 88
Computer 78
GROUP BY clause with MAX function
We can also use the MAX aggregate function with the GROUP BY clause in Structured Query
Language.
Syntax of Group BY clause with MAX aggregate function:
SELECT Column_Name_1, MAX(Column_Name) FROM Table_Name GROUP BY Column_
Name_1;
Example of MAX aggregate Function with GROUP BY Clause
This example takes the below School_Stu_Details table to understand the concept of GROUP
BY clause with SQL MAX aggregate function:
CREATE TABLE School_Stu_Details
(
Stu_ID INT NOT NULL,
Stu_Name varchar(100),
Stu_Subject varchar(50),
Stu_Age INT,
Stu_Marks INT
);
The following INSERT INTO statements insert the record of School students:
INSERT INTO School_Stu_Details VALUES (101, Anuj, English, 20, 88);
INSERT INTO School_Stu_Details VALUES (102, Raman, Maths, 24, 98);
INSERT INTO School_Stu_Details VALUES (104, Shyam, Hindi, 19, 92);
INSERT INTO School_Stu_Details VALUES (107, Vikash, Computer, 20, 78);
INSERT INTO School_Stu_Details VALUES (111, Monu, English, 21, 65);

Using Basic Structured Query Language Compiled by Bantegizia.Z


INSERT INTO School_Stu_Details VALUES (114, Jones, Hindi, 18, 93);
INSERT INTO School_Stu_Details VALUES (121, Parul, Maths, 20, 97);
INSERT INTO School_Stu_Details VALUES (123, Divya, English, 21, 89);
INSERT INTO School_Stu_Details VALUES (128, Hemant, Computer, 23, 90);
INSERT INTO School_Stu_Details VALUES (130, Nidhi, Hindi, 20, 88);
INSERT INTO School_Stu_Details VALUES (132, Priya, English, 22, 99);
INSERT INTO School_Stu_Details VALUES (138, Mohit, Maths, 21, 92);

The following query simply shows the record of students in the tabular form:
SELECT * FROM School_Stu_Details;
Stu_ID Stu_Name Stu_Subject Stu_Age Stu_Marks
101 Anuj English 20 88
102 Raman Maths 24 98
104 Shyam Hindi 19 92
107 Vikash Computer 20 78
111 Monu English 21 65
114 Jones Hindi 18 93
121 Parul Maths 20 97
123 Divya English 21 89
128 Hemant Computer 23 90
130 Nidhi Hindi 20 88
132 Priya English 22 99
138 Mohit Maths 21 92

The following query shows the maximum marks of a student in each subject from the above
School_Stu_Details table:

SELECT Stu_Subject, MAX (Stu_Marks) FROM School_Stu_Details GROUP BY Stu_Subject;


Output:
Stu_Subject MAX (Stu Marks)
English 99
Maths 98
Hindi 93
Computer 90

3.3 Filtering aggregate data using clause


LO4. Write and execute SQL sub-queries
4.1 Constructing single and nested sub-queries
What is subquery in SQL?
A subquery is a SQL query nested inside a larger query.
 A subquery may occur in :
 A SELECT clause
 A FROM clause

Using Basic Structured Query Language Compiled by Bantegizia.Z


 A WHERE clause
 The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE
statement or inside another subquery.
 A subquery is usually added within the WHERE Clause of another SQL SELECT
statement.
 You can use the comparison operators, such as >, <, or =. The comparison operator can
also be a multiple-row operator, such as IN, ANY, or ALL.
 A subquery is also called an inner query or inner select, while the statement containing a
subquery is also called an outer query or outer select.
 The inner query executes first before its parent query so that the results of an inner query
can be passed to the outer query.
You can use a subquery in a SELECT, INSERT, DELETE, or UPDATE statement to perform
the following tasks:
 Compare an expression to the result of the query.
 Determine if an expression is included in the results of the query.
 Check whether the query selects any rows.

 The subquery (inner query) executes once before the main query (outer query) executes.
 The main query (outer query) use the subquery result.

SQL Subqueries Example :


In this section, you will learn the requirements of using subqueries. We have the following two
tables 'student' and 'marks' with common field 'StudentID'.

Using Basic Structured Query Language Compiled by Bantegizia.Z


student

marks
Now we want to write a query to identify all students who get better marks than that of the
student who's StudentID is 'V002', but we do not know the marks of 'V002'.
- To solve the problem, we require two queries. One query returns the marks (stored in
Total_marks field) of 'V002' and a second query identifies the students who get better marks than
the result of the first query.

First query:
SELECT * FROM `marks`WHERE studentid = 'V002';
Copy

Query result:

The result of the query is 80.


- Using the result of this query, here we have written another query to identify the students who
get better marks than 80. Here is the query :

Second query:

SELECT a.studentid, a.name, b.total_marks


FROM student a, marks b
WHERE a.studentid = b.studentid
AND b.total_marks >80;
Query result:

Using Basic Structured Query Language Compiled by Bantegizia.Z


Above two queries identified students who get the better number than the student who's
StudentID is 'V002' (Abhay).

You can combine the above two queries by placing one query inside the other. The subquery
(also called the 'inner query') is the query inside the parentheses. See the following code and
query result :
SQL Code:
SELECT a.studentid, a.name, b.total_marks
FROM student a, marks b
WHERE a.studentid = b.studentid AND b.total_marks >
(SELECT total_marks
FROM marks
WHERE studentid = 'V002');
Copy
Query result:

Subqueries: General Rules


A subquery SELECT statement is almost similar to the SELECT statement and it is used to begin
a regular or outer query. Here is the syntax of a subquery:
Syntax:
(SELECT [DISTINCT] subquery_select_argument
FROM {table_name | view_name}
{table_name | view_name} ...
[WHERE search_conditions]
[GROUP BY aggregate_expression [, aggregate_expression] ...]
[HAVING search_conditions])
Subqueries: Guidelines
There are some guidelines to consider when using subqueries :
 A subquery must be enclosed in parentheses.
 A subquery must be placed on the right side of the comparison operator.
 Subqueries cannot manipulate their results internally, therefore ORDER BY clause
cannot be added into a subquery. You can use an ORDER BY clause in the main
SELECT statement (outer query) which will be the last clause.

Using Basic Structured Query Language Compiled by Bantegizia.Z


 Use single-row operators with single-row subqueries.
 If a subquery (inner query) returns a null value to the outer query, the outer query will not
return any rows when using certain comparison operators in a WHERE clause.
Type of Subqueries
 Single row subquery : Returns zero or one row.
 Multiple row subquery : Returns one or more rows.
 Multiple column subqueries : Returns one or more columns.
 Correlated subqueries : Reference one or more columns in the outer SQL statement. The
subquery is known as a correlated subquery because the subquery is related to the outer
SQL statement.
 Nested subqueries : Subqueries are placed within another subquery.
In the next session, we have thoroughly discussed the above topics. Apart from the above type of
subqueries, you can use a subquery inside INSERT, UPDATE and DELETE statement. Here is a
brief discussion :

Subqueries with INSERT statement


INSERT statement can be used with subqueries. Here are the syntax and an example of
subqueries using INSERT statement.

INSERT INTO neworder SELECT * FROM orders WHERE advance_amount


in(2000,5000);

Subqueries with UPDATE statement


In a UPDATE statement, you can set new column value equal to the result returned by a single
row subquery.

Here are the syntax and an example of subqueries using UPDATE statement.
UPDATE neworder SET ord_date='15-JAN-10' WHERE ord_amount-
advance_amount<(SELECT MIN(ord_amount) FROM orders);

Subqueries with DELETE statement


DELETE statement can be used with subqueries. Here are the syntax and an example of
subqueries using DELETE statement.
DELETE FROM neworder WHERE advance_amount<(SELECT MAX(advance_amount)
FROM orders);

4.2 Constructing sub-queries that return single row and multiple row

Single Row Subqueries in WHERE clause


You can place a subquery in the WHERE clause of another query. Let's take an example of a
query that contains a subquery placed in it's WHERE clause.
SELECT agent_name, agent_code, phone_no
FROM agents
WHERE agent_code =
(SELECT agent_code
FROM agents

Using Basic Structured Query Language Compiled by Bantegizia.Z


WHERE agent_name = 'Alex');
SELECT agent_code
FROM agents
WHERE agent_name = 'Alex';
SELECT agent_name, agent_code, phone_no
FROM agents
WHERE agent_code = 'A003';

Using comparison operators in Single Row subqueries


The previous example used the equality operator (=) in the WHERE clause. Other comparison
operators such as <>, >, <, <= can be used with a single subquery. The following example uses '
<' operator in the outer query WHERE clause. The AVG() function is used in the subquery to get
the average order amount, which is passed to the WHERE clause of the outer query. The final
result of the entire query is to get the 'ord_num', 'ord_amount', 'ord_date', 'cust_code' and
'agent_code' with following conditions:
Condition in outer query:
the 'ord_amount' of 'orders' table must be greater than the average 'ord_amount' of 'orders' table with
following condition an inner join.
SELECT ord_num,ord_amount,ord_date,cust_code, agent_code
FROM orders
WHERE ord_amount>
(SELECT AVG(ord_amount)
FROM orders
WHERE ord_date='20-APR-08');
SELECT AVG(ord_amount)
FROM orders
WHERE ord_date='20-APR-08';

The above query returns the average 'ord_amount' 2500, is used in the WHERE clause of the
outer query shown earlier.

SELECT ord_num,ord_amount,ord_date,cust_code, agent_code


FROM orders
WHERE ord_amount>2500;

Subqueries in a HAVING clause


HAVING clause is used to filter groups of rows. You may place a subquery in HAVING clause
in an outer query. This allows you to filter groups of rows based on the result returned by your
subquery. The following example uses a subquery in the HAVING clause of the outer query.
This example retrieves 'ord_amount, number of agent_codes and agent_code' from the table
orders with following conditions:
- agent_code of orders table must come distinctly.
- an average of ord_amount of each group of agent_code in orders table must be equal to the average
ord_amount of orders table.
- agent_code of orders table must be 'A008'.

Using Basic Structured Query Language Compiled by Bantegizia.Z


SELECT AVG(ord_amount),COUNT(agent_code),agent_code
FROM orders
GROUP BY agent_code
HAVING AVG(ord_amount)=
(SELECT AVG(ord_amount)
FROM orders
WHERE agent_code='A008');
SELECT AVG(ord_amount)
FROM orders
WHERE agent_code='A008';
SELECT AVG(ord_amount),COUNT(agent_code),agent_code
FROM orders
GROUP BY agent_code
HAVING AVG(ord_amount)=2500;

Subqueries in a FROM clause


You may place a subquery in the FROM clause of an outer query. These types of subqueries are
also known is inline views because the subquery provides data inline with the FROM clause. The
following example retrieves the item_id whose item_id is less than 4.
SELECT item_id
FROM
(SELECT item_id
FROM FOODS
WHERE item_id<4)

Error in Single Row Subqueries


In this section, we will discuss some errors you might face in a 'single row subquery' operation.
In our previous examples, we have seen, a single row subquery always returns a single row and
if a subquery returns more than one row then an error occurs. In the following example, the
subquery attempts to pass multiple rows to the equality operator (=) in the outer query.
SELECT item_id, item_name
FROM foods
WHERE item_id =
(SELECT item_id
FROM foods
WHERE item_name LIKE '%a%');
SELECT item_id
FROM foods
WHERE item_name LIKE '%a%';
Nested subqueries
A subquery can be nested inside other subqueries. SQL has an ability to nest queries within one
another. A subquery is a SELECT statement that is nested within another SELECT statement and
which return intermediate results. SQL executes innermost subquery first, then next level. See
the following examples :

Using Basic Structured Query Language Compiled by Bantegizia.Z


Example -1 : Nested subqueries
If we want to retrieve that unique job_id and there average salary from the employees table
which unique job_id have a salary is smaller than (the maximum of averages of min_salary of
each unique job_id from the jobs table which job_id are in the list, picking from (the job_history
table which is within the department_id 50 and 100)) the following SQL statement can be used :
SELECT job_id,AVG(salary)
FROM employees
GROUP BY job_id
HAVING AVG(salary)<
(SELECT MAX(AVG(min_salary))
FROM jobs
WHERE job_id IN
(SELECT job_id FROM job_history
WHERE department_id
BETWEEN 50 AND 100)
GROUP BY job_id);

SELECT MAX(AVG(min_salary))
FROM jobs
WHERE job_id
IN(
'ST_CLERK','ST_CLERK','IT_PROG',
'SA_REP','SA_MAN','AD_ASST', '
AC_ACCOUNT')
GROUP BY job_id;
The subquery returns the maximum of averages of min_salary for each unique job_id return ( i.e.
'ST_CLERK','ST_CLERK','IT_PROG', 'SA_REP','SA_MAN','AD_ASST', 'AC_ACCOUNT' )
by the previous subquery.
Now the outer query that receives output from the subquery and which also receives the output
from the nested subquery stated above.

SELECT job_id,AVG(salary) FROM employees GROUP BY job_id HAVING


AVG(salary)<10000;

The outer query returns the job_id, average salary of employees that are less than maximum of
average of min_salary returned by the previous query

4.3 Using correlated sub-queries to retrieve required data


SQL Correlated Subqueries
Correlated subqueries are used for row-by-row processing. Each subquery is executed once for
every row of the outer query.

Using Basic Structured Query Language Compiled by Bantegizia.Z


A correlated subquery is evaluated once for each row processed by the parent statement. The
parent statement can be a SELECT, UPDATE, or DELETE statement.

SELECT column1, column2, ....


FROM table1 outer
WHERE column1 operator
(SELECT column1, column2
FROM table2
WHERE expr1 =
outer.expr2);
A correlated subquery is one way of reading every row in a table and comparing values in each
row against related data. It is used whenever a subquery must return a different result or set of
results for each candidate row considered by the main query. In other words, you can use a
correlated subquery to answer a multipart question whose answer depends on the value in each
row processed by the parent statement.
Nested Subqueries Versus Correlated Subqueries :
With a normal nested subquery, the inner SELECT query runs first and executes once, returning
values to be used by the main query. A correlated subquery, however, executes once for each
candidate row considered by the outer query. In other words, the inner query is driven by the
outer query.
NOTE: You can also use the ANY and ALL operator in a correlated subquery. EXAMPLE of
Correlated Subqueries : Find all the employees who earn more than the average salary in their
department.

SELECT last_name, salary, department_id


FROM employees outer
WHERE salary >
(SELECT AVG(salary)
FROM employees

Using Basic Structured Query Language Compiled by Bantegizia.Z


WHERE department_id =
outer.department_id group by department_id);

Other use of correlation is in UPDATE and DELETE


CORRELATED UPDATE :
UPDATE table1 alias1
SET column = (SELECT expression
FROM table2 alias2
WHERE alias1.column =
alias2.column);
Use a correlated subquery to update rows in one table based on rows from another table.

CORRELATED DELETE :
DELETE FROM table1 alias1
WHERE column1 operator
(SELECT expression
FROM table2 alias2
WHERE alias1.column = alias2.column);
Use a correlated subquery to delete rows in one table based on the rows from another
table.

Using the EXISTS Operator :


The EXISTS operator tests for existence of rows in the results set of the subquery. If a
subquery row value is found the condition is flagged TRUE and the search does not
continue in the inner query, and if it is not found then the condition is
flagged FALSE and the search continues in the inner query.

EXAMPLE of using EXIST operator :


Find employees who have at least one person reporting to them.

SELECT employee_id, last_name, job_id, department_id


FROM employees outer
WHERE EXISTS ( SELECT ’X’
FROM employees
WHERE manager_id =
outer.employee_id);

Using Basic Structured Query Language Compiled by Bantegizia.Z


OUTPUT :

EXAMPLE of using NOT EXIST operator :


Find all departments that do not have any employees.

SELECT department_id, department_name


FROM departments d
WHERE NOT EXISTS (SELECT ’X’
FROM employees
WHERE department_id
= d.department_id);
OUTPUT :

4.4 Writing sub-queries using aggregates

Using Basic Structured Query Language Compiled by Bantegizia.Z

You might also like