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

hiya dbms

This document outlines SQL commands and concepts, focusing on the ORDER BY, GROUP BY, HAVING clauses, and the use of ANY, ALL, and EXISTS operators. It provides examples of SQL queries to retrieve and manipulate data from tables, including sorting, grouping, and filtering results based on specific conditions. Additionally, it includes various exercises related to faculty and department data to reinforce the learning outcomes.

Uploaded by

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

hiya dbms

This document outlines SQL commands and concepts, focusing on the ORDER BY, GROUP BY, HAVING clauses, and the use of ANY, ALL, and EXISTS operators. It provides examples of SQL queries to retrieve and manipulate data from tables, including sorting, grouping, and filtering results based on specific conditions. Additionally, it includes various exercises related to faculty and department data to reinforce the learning outcomes.

Uploaded by

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

02217711723.

01417711723 MANVI.
Hiya Naval Kamboj IIOT

Experiment 5

AIM:

Theory and Concepts:

1. ORDER BY
The ORDER BY command is used to sort the result set in ascending or descending order.
The ORDER BY command sorts the result set in ascending order by default. To sort the
records in descending order, use the DESC keyword.
ASC: The ASC command is used to sort the data returned in ascending order.

The following SQL statement selects all the columns from the "Customers" table, sorted by
the "CustomerName" column:

Example: SELECT * FROM Customers


ORDER BY CustomerName ASC;

DESC: The DESC command is used to sort the data returned in descending order.
The following SQL statement selects all the columns from the "Customers" table, sorted
descending by the "CustomerName" column:
Example: SELECT * FROM Customers
ORDER BY CustomerName DESC;
2.The SQL GROUP BY Statement

The GROUP BY statement groups rows that have the same values into summary rows, like
"find the number of customers in each country".

The GROUP BY statement is often used with aggregate functions


(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
In many cases, we want to apply the aggregate functions to subgroups of tuples in a relation

Each subgroup of tuples consists of the set of tuples that have the same value for the grouping
attribute(s)
02217711723.
01417711723 MANVI.
Hiya Naval Kamboj IIOT

The function is applied to each subgroup independently


SQL has a GROUP BY-clause for specifying the grouping attributes, which must also
appear in the SELECT-clause

Query : For each department, retrieve the department number, the number of employees in
the department, and their average salary.

SELECT DNO, COUNT (*), AVG (SALARY) FROM EMPLOYEE GROUP BY DNO
In this the EMPLOYEE tuples are divided into groups. Each group having the same
value for the grouping attribute DNO
The COUNT and AVG functions are applied to each such group of tuples separately
The SELECT-clause includes only the grouping attribute and the functions to be applied
on each group of tuples
A join condition can be used in conjunction with grouping

THE HAVING-CLAUSE
Sometimes we want to retrieve the values of these functions for only those groups that satisfy
certain conditions. The HAVING-clause is used for specifying a selection condition on
groups
(rather than on individual tuples)
Query :For each project on which more than two employees work, retrieve the project
number, project name, and the number of employees who work on that project.

SELECT PNUMBER, PNAME, COUNT (*) FROM PROJECT, WORKS_ON


WHERE PNUMBER=PNO GROUP BY PNUMBER, PNAME
HAVING COUNT (*) > 2

3. ANY and ALL Operators

The ANY and ALL operators allow you to perform a comparison between a single column
value and a range of other values.

ANY means that the condition will be true if the operation is true for any of the values in the
range.

ALL means that the condition will be true only if the operation is true for all values in the
range.

ANY Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name
01417711723
02217711723. Hiya Naval Kamboj
MANVI. IIOT

FROM table_name
WHERE condition);
ALL Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name
FROM table_name
WHERE condition);

1. The SQL EXISTS condition is used in combination with a subquery and is considered
to be met, if the subquery returns at least one row. It can be used in a SELECT, INSERT,
UPDATE, or DELETE statement.

2. The LIKE comparison operator is used to compare partial strings. Two reserved
characters are used: '%' (or '*' in some implementations) replaces an arbitrary number of
characters, and '_' replaces a single arbitrary character.
Query: Retrieve all employees whose address is in Houston, Texas. Here, the value of
the
ADDRESS attribute must contain the substring 'Houston,TX‘ in it.

SELECT FNAME, LNAME FROM EMPLOYEE WHERE ADDRESS LIKE


'%Houston,TX%'

SQL Commands:

teacher(fid, name, dno, sal, address, phone, dob, exp)


dept(Dno, Dname, Budget)
01417711723
02217711723. Hiya Naval Kamboj
MANVI. IIOT

• Retrieve the id, name and salary of faculty in increasing order of salary.

• Retrieve the id, name and salary of faculty in decreasing order of experience.
• Retrieve the id, name and salary of faculty in increasing order of salary and decreasing order
of experience.
• Show the average experience department wise.

• Retrieve the maximum salary department wise.


• Retrieve the average salary department wise having average salary greater than 5000.
• Retrieve faculties details where name (table should have names accordingly)
•Starting with ‘S’
• Ending with ‘n’

• Starting with ‘a’ and ending with ‘h’


• Must contain ‘a’

• Should have 5 letter only starting with ‘g’


• Show the use of any/all clause: List the faculties of department whose budget is less

than 10000 (select name,dno from faculty where dno < any/all (select dno from dept
where budget <10000);)

• Exist/not exist (select name,dno from faculty where exists (select dno from dept
where faculty.dno=dept.dno and budget>7000);

• Find the names of faculty who work in department AIML (using subquery).
THEORY:

Retrieve the id, name and salary of faculty in increasing order of salary.
02217711723.
01417711723 MANVI.
Hiya Naval Kamboj IIOT

Retrieve the id, name and salary of faculty in decreasing order of experience.

Retrieve the id, name and salary of faculty in increasing order of salary and decreasing order
of experience
02217711723.
01417711723 MANVI.
Hiya Naval Kamboj IIOT

Show the average experience department wise.

Retrieve the maximum salary department wise.

Retrieve the average salary department wise having average salary greater than 5000.

Retrieve faculties details where name (table should have names accordingly):
Starting with ‘S’
02217711723.
01417711723 MANVI.
Hiya Naval Kamboj IIOT

Ending with ‘N’

Starting with ‘A’ and ending with ‘H’

Must contain ‘A’

Should have 5 letter only starting with ‘G’


02217711723. MANVI. IIOT

Show the use of any/all clause: List the faculties of department whose budget is less than
10000

Exist/ not exist (select name,dno from faculty where exists (select dno from dept where
faculty.dno=dept.dno and budget>7000);

Find the names of faculty who work in department AIML (using subquery).

LEARNING OUTCOMES:

You might also like