hiya dbms
hiya dbms
01417711723 MANVI.
Hiya Naval Kamboj IIOT
Experiment 5
AIM:
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:
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".
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
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.
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.
SQL Commands:
• 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.
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
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
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: