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

Ch7 SQL (Basics)

SQL is a standard language used to create, manipulate, and retrieve data from relational database management systems. It has several types of commands including DDL for defining database schema, DML for manipulating data, DCL for controlling access, and DQL for querying data. Key SQL commands include CREATE to build database objects, INSERT to add data, SELECT to retrieve data, UPDATE and DELETE to modify data.

Uploaded by

akumar51be23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Ch7 SQL (Basics)

SQL is a standard language used to create, manipulate, and retrieve data from relational database management systems. It has several types of commands including DDL for defining database schema, DML for manipulating data, DCL for controlling access, and DQL for querying data. Key SQL commands include CREATE to build database objects, INSERT to add data, SELECT to retrieve data, UPDATE and DELETE to modify data.

Uploaded by

akumar51be23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

Structured Query Language

(SQL)

Data Types: Click here to Read Oracle’s Data Types

1
UCS310-Database Management System
Introduction to SQL
What is SQL?
– SQL stands for Structured Query Language.
– SQL is used to create, remove, alter the database and database
objects in a database management system and to store,
retrieve, update the data in a database.
– SQL is a standard language for creating, accessing,
manipulating database management system.

UCS310-Database Management System


2
Introduction to SQL
Concept of SQL

– The user specifies a certain condition.

– The program will go through all the records in the database file
and select those records that satisfy the condition (searching).

– Statistical information of the data.

– The result of the query will then be stored in form of a table.

UCS310-Database Management System


3
Introduction to SQL
• Different Types of SQL Commands:
• DDL (Data Definition Language).
• DML (Data Manipulation Language).
• DCL (Data Control Language).
• TCL (Transaction Control Language).
• DQL (Data Query Language).

UCS310-Database Management System


4
DDL: Data Definition Language
All DDL commands are auto-committed. That means it saves all the changes
permanently in the database.

Command Description
Create to create new table or database
Alter for alteration
Truncate delete data from table
Drop to drop a table
Rename to rename a table

UCS310-Database Management System 5


DML: Data Manipulation Language
DML commands are not auto-committed. It means changes are not permanent to
database, they can be rolled back.

Command Description
Insert to insert a new row
Update to update existing row
Delete to delete a row
Merge merging two rows or two tables

UCS310-Database Management System 6


TCL: Transaction Control Language
These commands are to keep a check on other commands and their effect on the
database. These commands can annul changes made by other commands by rolling
back to original state. It can also make changes permanent.
.

Command Description
Commit to permanently save
Rollback to undo change
Savepoint to save temporarily

UCS310-Database Management System 7


DCL : Data Control Language
Data control language provides command to grant and take back authority.

Command Description
Grant grant permission of right
Revoke take back permission.

UCS310-Database Management System 8


Data Definition Language
• DDL consists of the SQL commands that can be
used to define the database schema.
• Deals with descriptions of the database schema
• Used to create and modify the structure of database
objects in the database.
• List of Commands: Create, Drop, and Alter

UCS310-Database Management System 9


Data Definition Language
Create: It is used to create a table
Syntax: Create table tablename (column1 data_type
(size), column2 data_type (size)…);
Query:
Create table Customer (CustomerId number,
CustomerName char(20), ContactNo number, address
varchar (20), city char(20), Postalcode varchar(20),
country char (20);

UCS310-Database Management System 10


Data Definition Language
Alter command

alter command is used for alteration of table structures.


There are various uses of alter command, such as,
* to add a column to existing table
* to rename any existing column
* to change datatype of any column or to modify its size.
* alter is also used to drop a column.

UCS310-Database Management System 11


Data Definition Language
Alter command
To Add Column to existing Table
Using alter command we can add a column to an existing table. Following is
the Syntax,

alter table table-name add(column-name datatype);

Here is an Example for this,

alter table Students add (address varchar(50));


This command will add a new column address to the Students table

UCS310-Database Management System 12


Data Definition Language
Alter command
To Add Multiple Column to existing Table
Using alter command we can even add multiple columns to an existing table. Following is the
Syntax,

alter table table-name add(column-name1datatype1, column-name2datatype2);

Here is an Example for this,

alter table students add (dob number(10), city varchar(10));

UCS310-Database Management System 13


Data Definition Language
Alter command

To Add column with Default Value


Alter command can add a new column to an existing table with default values.
Following is the Syntax.

alter table table-name add(column-name1 datatype1 default


data);

alter table students add (branch varchar(10) default ‘COE');

UCS310-Database Management System 14


Data Definition Language
Alter command
To Modify an existing Column
alter command is used to modify data type of an existing column . Following is the
Syntax,
alter table table-name modify(column-name datatype);

Here is an Example for this,


alter table students modify (address char(30));
The above command will modify address column of the Students
table

UCS310-Database Management System 15


Data Definition Language
Alter command
To Rename a column

Using alter command you can rename an existing column.


Following is the Syntax,
alter table table-name rename column old-column-name to column-name;

Here is an Example for this,


alter table Student rename address to Location;

The above command will rename address column to Location.

UCS310-Database Management System 16


Data Definition Language
Alter command
To Drop a Column
alter command is also used to drop columns also.
Following is the Syntax,
alter table table-name drop(column-name);

Here is an Example for this,

alter table Students drop (address);

UCS310-Database Management System 17


Data Definition Language
Truncate command
Truncate command removes all records from a table. But this command will not
destroy the table's structure. When we apply truncate command on a table its Primary
key is initialized. Following is its Syntax,

truncate table table-name

Here is an Example explaining it.

truncate table students;

The above query will delete all the records of students table.

UCS310-Database Management System 18


Data Definition Language
Drop command
Drop query completely removes a table from database. This command will also
destroy the table structure.
Following is its Syntax,
drop table table-name;
;
Here is an Example explaining it.
drop table students;

The above query will delete the students table completely. It can also be used on
Databases. For Example, to drop a database,

drop database Test;


The above query will drop a database named Test from the system

UCS310-Database Management System 19


Data Definition Language
Rename query
Rename command is used to rename a table. Following is its Syntax,

rename old_table_name to new_table_name;

Here is an Example explaining it.

rename students to people;

The above query will rename Students table to People.

UCS310-Database Management System 20


Data Manipulation Language
• The SQL commands that deals with the
manipulation of data present in the database.
• Insert, select, select distinct, update are DML
commands.
• Insert: Insert is used to insert values in the created
table
Syntax: Insert into tablename values (column1,
column2,…column);
Query: Insert into customer values(1, ‘Aman
Sharma’, ‘Obere Str. 57’,’Berlin’,12209,’Germany’);
UCS310-Database Management System 21
Data Manipulation Language
1) INSERT command

Insert command is used to insert data into a table. Following is its general
syntax,

INSERT into table-namevalues(data1,data2,..)

Consider a table Student with following fields.


Students(Roll, Name, Age)

insert into Students values(1, 'mani',22);


insert into Students values(2, 'mahi',24);
insert into Students values(3, 'appy',20);

UCS310-Database Management System 22


Data Manipulation Language
1) INSERT command
Example to Insert NULL value to a column
The statements below will insert NULL value into age column of
the Student table.

insert into students values(4,'padm',null);

Example to Insert Default value to a column


insert into students values(5,'rahul', default);

Suppose the age column of student table has default value of 18, then for the age field
18 value will ne added in new record.

UCS310-Database Management System 23


Data Manipulation Language
2) UPDATE command
Update command is used to update a row of a table. Following is its
general syntax,

UPDATEtable-name set column-name = value where condition;

Lets see an example,

update students set age=22 where id=4;

Example to Update multiple columns

update students set name='pri',age=25 where id=5;

UCS310-Database Management System 24


Data Manipulation Language
3) Delete command
Delete command is used to delete data from a table. Delete command can also be used
with condition to delete a particular row. Following is its general syntax,
DELETE from table-name;

Example to Delete all Records from a Table


delete from students;

The above command will delete all the records from Students table.
Example to Delete a particular Record from a Table

delete from students where id=5;

The above command will delete the record where id is 5 from Students table.

UCS310-Database Management System 25


Data Query Language

SQL Query: SELECT


 Select query is used to retrieve data from a tables.

 It is the most used SQL query.

 We can retrieve complete tables, or partial by mentioning conditions using


WHERE clause.

UCS310-Database Management System 26


Basic structure of an SQL query

UCS310-Database Management System 27


Example: North wind Sample
Dataset (Customer Table)
CustomerID CustomerName Contact Address City PostalCod Country
Name e
1 Aman Sharma Obere Str. 57 Berlin 12209 Germany

2 Rohan Garg Avda. de la México 05021 Mexico


Constitución D.F.
2222
3 Mohan Goel Mataderos México 05023 Mexico
2312 D.F.
4 Sohan wadhwa 120 Hanover London WA1 1DP UK
Sq.
5 Kawal Sahni Berguvsväge Luleå S-958 22 Sweden
n8
6 Aarohan Verma 125 Hanover London WA1 1DP UK
Sq.

The table above contains six records and seven columns (CustomerID,
CustomerName, ContactName, Address, City, PostalCode, and Country).

UCS310-Database Management System 28


General Structure: Select Statement
Select: It is used to extract data from a database.
Syntax: SELECT [* / DISTINCT] expr1 [col1], expr2 [col2] FROM
tablename WHERE condition;
- * will extract all columns of table including duplicates values.
- DISTINCT will extract only the value which are unique.
- expr1, expr2 can be a column name or any expression of a function.
- col1 and col2 are the particular column names you want to extract.
- tablename is the name of the table from which you want to extract
the values.
- Where clause is used where you want to extract columns of some
specific value.

UCS310-Database Management System 29


Examples: Select Statement
Note: We are using the Customer Table for all the examples.
eg 1. Select all values from customer
Query: Select * from Customer;
Output:

UCS310-Database Management System 30


Examples: Select Statement
Note: We are using the Customer Table for all the examples.
eg 2. Extract distinct city from customer table.
Query: Select DISTINCT City from Customer;
Output: The customer database consists of 6 rows in which 4 cities are
distinct. Therefore four cities are extracted as shown below.

City
Berlin

México D.F.
Luleå
London

UCS310-Database Management System 31


Examples: Select Statement
Note: We are using the Customer Table for all the examples.
eg 3. Extract customerName and Contact Name of Germany People
from customer table.
Query: Select customerName, ContactName from Customer where
country = ‘Germany’;
Output:

UCS310-Database Management System 32


Examples: Select Statement
Note: We are using the Customer Table for all the examples.
eg 4. Extract all values of customer id 1 from customer table.
Query: Select * from Customer where id = 1;
Output:

CustomerID CustomerName Contact Address City PostalCod Country


Name e
1 Aman Sharma Obere Str. 57 Berlin 12209 Germany

UCS310-Database Management System 33


Examples: Select Statement
Note: We are using the Customer Table for all the examples.
eg 5. Extract Customer name, Contact name and postal code of Mexico
D.F city of Mexico People.
Query: Select CustomerName, ContactName, PostalCode from
Customer where City=‘Mexico D.F.’ AND Country=‘Mexico’;
Output:
CustomerName Contact PostalCod
Name e
Rohan Garg 05021
Mohan Goel 05023

UCS310-Database Management System 34


Update Statement
Update Command is used to update the records in the table
Syntax: UPDATE table_name SET column1 = value1, column2 =
value2...., columnN = valueN WHERE [condition];
Query: Update Customer set CustomerName=‘abc’, where
City=‘Berlin.’ AND Country=‘Germany’;
Output:

CustomerID CustomerName Contact Address City PostalCod Country


Name e
1 abc Sharma Obere Str. 57 Berlin 12209 Germany

UCS310-Database Management System 35


.
.

Comparison
1. IN: The IN command allows you to specify multiple values in
a WHERE clause. The IN operator is a shorthand for multiple
OR conditions.
Syntax: Select col1 from tablename Where IN ( value1,
value2, value3);
2. Between: The BETWEEN command is used to select values
within a given range. The values can be numbers, text, or
dates.
Syntax: Select col1 from tablename Where condition
BETWEEN Value1 AND Value2;
3. Like: The LIKE command is used in a WHERE clause to
search for a specified pattern in a column.
Syntax: Select col1 from tablename Where col like ‘val1’;

UCS310-Database Management System 36


.
.

Comparison (IN)
Note: We are using the Customer Table for all the examples.
eg 1. Extract all values from customer table of id 1, 4 and 5.
Query: Select * from Customer where id IN (1,4,5);
Output:

UCS310-Database Management System 37


.
.

Comparison (Not IN)


Note: We are using the Customer Table for all the examples.
eg 2. Extract all values from customer table who are not from
London and Berlin.
Query: Select * from Customer where city NOT IN (‘London’,
Berlin’);
Output:
CustomerID CustomerName Contact Address City PostalCod Country
Name e
2 Rohan Garg Avda. de la México 05021 Mexico
Constitución D.F.
2222
3 Mohan Goel Mataderos México 05023 Mexico
2312 D.F.
5 Kawal Sahni Berguvsväge Luleå S-958 22 Sweden
n8

UCS310-Database Management System 38


.
.

Comparison (Between)
Note: We are using the Customer Table for all the examples.
eg 3. Extract all values from customer table between 2 to 5.
Query: Select * from Customer where CustomerId Between 2 and 5;
Output:

CustomerID CustomerName Contact Address City PostalCod Country


Name e
3 Mohan Goel Mataderos México 05023 Mexico
2312 D.F.
4 Sohan wadhwa 120 Hanover London WA1 1DP UK
Sq.

UCS310-Database Management System 39


.
. Comparison (Like)
Note: We are using the Customer Table for all the examples.
eg 4. Extract all values from customer table with a CustomerName
ending with n.
Query: Select * from Customer where CustomerName LIKE ‘%n’
Output:

CustomerID CustomerName Contact Address City PostalCod Country


Name e
1 Aman Sharma Obere Str. 57 Berlin 12209 Germany

2 Rohan Garg Avda. de la México 05021 Mexico


Constitución D.F.
2222
3 Mohan Goel Mataderos México 05023 Mexico
2312 D.F.
4 Sohan wadhwa 120 Hanover London WA1 1DP UK
Sq.
6 Aarohan Verma 125 Hanover London WA1 1DP UK
UCS310-Database Management
Sq. System 40
.
.

Comparison (LIKE)
Note: We are using the Customer Table for all the examples.
eg 5. Extract all values from customer table with a
CustomerName starting with a.
Query: Select * from Customer where CustomerName LIKE ‘a
%’
Output:CustomerName
CustomerID Contact Address City PostalCod Country
Name e
1 Aman Sharma Obere Str. 57 Berlin 12209 Germany

6 Aarohan Verma 125 Hanover London WA1 1DP UK


Sq.

UCS310-Database Management System 41


.
.

The following SQL (LIKE)


Note: We are using the Customer Table for all the examples.
eg 6. Selects all customers with a CustomerName starting with a.
Query: Select * from Customer where CustomerName LIKE ‘a
%’
Output:
CustomerID CustomerName Contact Address City PostalCod Country
Name e
1 Aman Sharma Obere Str. 57 Berlin 12209 Germany

6 Aarohan Verma 125 Hanover London WA1 1DP UK


Sq.

UCS310-Database Management System 42


.
.

The following SQL (LIKE)


Note: We are using the Customer Table for all the examples.
eg 6. Selects all customers with a CustomerName that have “ha"
in any position.
Query: Select * from Customer where CustomerName LIKE
‘%ha%’
Output:
CustomerID CustomerName Contact Address City PostalCod Country
Name e
2 Rohan Garg Avda. de la México 05021 Mexico
Constitución D.F.
2222
3 Mohan Goel Mataderos México 05023 Mexico
2312 D.F.
4 Sohan wadhwa 120 Hanover London WA1 1DP UK
Sq.
6 Aarohan Verma 125 Hanover London WA1 1DP UK
Sq.
UCS310-Database Management System 43
Logical Operators AND, OR,
NOT
• The AND, OR and NOT operators are used to filter
records based on more than one condition:
• The AND operator displays a record if all the conditions
separated by AND are TRUE.
• The OR operator displays a record if any of the
conditions separated by OR is TRUE.
• The NOT operator displays a record if the condition(s) is
NOT TRUE.

UCS310-Database Management System 44


AND
Syntax: SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
Query: Select DeptId and Salary from employee where
name=‘Anjali’ and city=‘Bangalore’
Output:
DeptId Salary
1014 58000

UCS310-Database Management System 45


OR
Syntax: SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
Query: Select DeptId and Salary from employee where
name=‘Anjali’ or city=‘Bangalore’
Output:
DeptId Salary
1014 58000
1003 34000
1014 45000

UCS310-Database Management System 46


NOT
Syntax: SELECT column1, column2, ...
FROM table_name WHERE NOT condition1;
Query: Select DeptId and Salary from employee where
city< >‘Bangalore’
Output:

UCS310-Database Management System 47


Set Operations in SQL

SQL supports few Set operations which can be


performed on the table data. These are used to
get meaningful results from data stored in the
table, under different special conditions.
• UNION
• UNION ALL
• INTERSECT
• MINUS
Union Operation in SQL
UNION is used to combine the results of two or more SELECT statements. However, it
will eliminate duplicate rows from its result set. In case of union, number of columns and
datatype must be same in both the tables, on which UNION operation is being applied.

The union of A and B (AB)

A B

A table containing all the rows from A and B.

UCS310-Database Management System 49


Union

Syntax: SELECT ...... FROM ...... WHERE ......


UNION
SELECT ...... FROM ...... WHERE ......;

UCS310-Database Management System 50


Table1 Table2
ID Name ID Name
1 abhi 2 adam
2 adam 3 chester

SELECT * FROM Table1 UNION SELECT *


FROM Table2;

OUTPUT ID NAME
1 abhi
2 adam
3 chester
Union All
This operation is similar to Union. But it also shows the duplicate rows.

SELECT * FROM Table1 UNION ALL SELECT * FROM Table2;

OUTPUT
ID NAME
1 abhi
2 adam
2 adam
3 chester
Intersection
Intersect operation is used to combine two SELECT statements, but it
only returns the records which are common from
both SELECT statements. In case of Intersect the number of columns
and datatype must be same.

SELECT * FROM Table1 INTERECT SELECT * FROM


Table2;
OUTPUT
ID NAME
2 adam
Difference or Minus
The Minus operation combines results of two SELECT statements
and return only those in the final result, which belongs to the first
set of the result.

SELECT * FROM Table1 MINUS SELECT * FROM


Table2;
OUTPUT ID NAME

1 abhi
More Examples

Customer Employee
first_name last_name first_name last_name
Stephen Jones Christina Jones
Mark Smith Michael McDonald
Denise King Paula Johnson
Paula Johnson Stephen Jones
Richard Archer Richard Smith

UCS310-Database Management System 55


Union
eg 1. Combine both customer and employee table.
Query:
Select first_name, last_name from Customer
UNION
Select first_name, last_name from Employee

56
Union
Output:

First_name last_name
Stephen Jones
Mark Smith
Denise King
Paula Johnson
Richard Archer
Christina Jones
Michael McDonald
Richard Smith

UCS310-Database Management System 57


Union
Example 2. Combine both customer and employee table in ascending order.
Query:
Select first_name, last_name from Customer
UNION
Select first_name, last_name from Employee Order

UCS310-Database Management System 58


Intersection
SELECT ...... FROM table1 ;
Intersect
SELECT col FROM table2
eg 2. Find common first_name and last_name from both customer
and employee table.
Query:
Select first_name, last_name from customer
Intersect
Select first_name, last_name from customer;

UCS310-Database Management System 59


Intersection
Output:

FIRST_NAME LAST_NAME
Paula johnson
Stephen jones

You can also use IN to find intersection

UCS310-Database Management System 60


Difference of Tables
SELECT ...... FROM table1 ;
SELECT col FROM table2 )
eg 3. Find first name and last name from customer which are not in
employee.
Query:
Select * from customer
MINUS
Select * from employee;

UCS310-Database Management System 61


Difference of Tables
Output:

FIRST_NAME LAST_NAME
Denise king
Mark Smith
Richard Archer

You can also use NOT IN to find difference.

UCS310-Database Management System 62


Grouping
eg. 11 List the number of students of each class.

UCS310-Database Management System 63


Group By Class
class
1A

1A 1A
1A
COUNT( )

1B
1B
1B
1B 1B
COUNT( )
1B
1B
1C

1C 1C
1C
COUNT( )

Student
UCS310-Database Management System 64
Grouping
eg. 11 List the number of students of each class.
SELECT class, COUNT(*) FROM student
GROUP BY class;

Result

UCS310-Database Management System 65


Grouping
eg. 12 List the average Math test score of each class.

UCS310-Database Management System 66


Group By Class
class
1A

1A 1A
1A
AVG( )
1B
1B

1B
1B
1B
AVG( )
1B
1B
1C

1C 1C AVG( )
1C
Student
UCS310-Database Management System 67
Grouping
eg. 12 List the average Math test score of each class.

SELECT class, AVG(mtest) FROM student


GROUP BY class;

Result

UCS310-Database Management System 68


Grouping, Cont’d…
The GROUP BY command is used to group the result set (used
with aggregate functions: COUNT, MAX, MIN, SUM, AVG).
Syntax: SELECT ...... FROM ...... WHERE condition
GROUP BY groupexpr [HAVING requirement];

– groupexpr specifies the related rows to be grouped as one


entry. Usually it is a column.

– WHERE condition specifies the condition of individual rows


before the rows are group. HAVING requirement specifies the
condition involving the whole group.

UCS310-Database Management System 69


Grouping
Note: Consider the following table for further queries .
ID Dept_Id Name City Salary
1 1014 Ajay Bangalore 58000
2 1014 Harry Delhi 29001
3 1003 Anjali Bangalore 34000
4 1023 Rahul Delhi 54000
5 1003 Anisha Chennai 67000
6 1023 Rajni Mumbai 56000
7 1003 Zaid Mumbai 29000
8 1249 Bib NY 87000
9 1014 Raj Bangalore 45000
10 1003 Arya Chennai 64000

UCS310-Database Management System 70


Grouping (AVG)
eg 1. find the average salary for each department’s employees
Query: Select Dept_Id, AVG (Salary) from Employee Group BY
Dept_ID
Output:

Dept_ID AVG (Salary)


1014 44000.3333
1003 48500.0000
1023 55000.0000
1249 87000.0000

UCS310-Database Management System 71


Grouping (Count)

eg 2. List the number of employees from each city


Query: Select City, COUNT(City) from Employee Group BY City;
Output:

City Count (City)


Bangalore 3
Delhi 2
Chennai 2
Mumbai 2
NY 1

UCS310-Database Management System 72


Grouping with Order By
eg 3. List the number of employees from each city in ascending order
Query: Select City, COUNT(City) from Employee Group BY City
ORDER By COUNT(City);
Output:
City Count (City)
NY 1
Delhi 2
Chennai 2
Mumbai 2
Bangalore 3

UCS310-Database Management System 73


Grouping with Order By
eg 4. List the number of employees from each city in descending order
Query: Select City, COUNT(City) from Employee Group BY City
ORDER By COUNT(City) DESC;
Output:
City Count (City)
Bangalore 3
Delhi 2
Chennai 2
Mumbai 2
NY 1

UCS310-Database Management System 74


Grouping (MAX)
eg 5. Find the maximum salary for each department with “Dept_ID”
greater than 1003.
Query: Select Dept_ID, MAX(Salary) from Employee where
Dept_ID>1003 Group BY Dept_ID;
Output:
Dept_ID MAX (Salary)
1014 58000
1023 56000
1249 87000

UCS310-Database Management System 75


Grouping (MIN)
eg 6. Find the minimum salary for each department with “Dept_ID”
greater than 1003.
Query: Select Dept_ID, MIN(Salary) from Employee where
Dept_ID>1003 Group BY Dept_ID;
Output:
Dept_ID MAX (Salary)
1014 29001
1023 54000
1249 87000

UCS310-Database Management System 76


Grouping (SUM)

eg 7. Find the total salary of all the employees:


Query: Select Count(Dept_ID), SUM(Salary) from Employee.
Output:
(Count) Dept_ID SUM (Salary)
10 5230001

UCS310-Database Management System 77


Grouping (Count)
eg 8. Find the number of employees from each city in all the
departments.
Query: Select City, Dept_ID, Count (*) from Employee Group By
City, Dept_ID Order By Count (*);
Output:
City Dept_ID Count (*)
Delhi 1014 1
Bangalore 1003 1
Delhi 1023 1
Mumbai 1023 1
Mumbai 1003 1
NY 1249 1
Bangalore 1014 2
Chennai 1003 Management System
UCS310-Database 2 78
Grouping with Having
eg 9. Find the cities with multiple employees.
Query: Select City, Count (City) from Employee Group By City
Having Count (City)>1 Order By Count (City);
Output:
City Count (City)
Delhi 2
Chennai 2
Mumbai 2
Bangalore 3

UCS310-Database Management System 79


UCS310-Database Management System 80
END

UCS310-Database Management System 81

You might also like