Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
73 views6 pages

DBMS Lab 07 04112020 012905pm

The document is a lab manual for a database management systems course, containing instructions and examples for using SQL functions like DISTINCT, ORDER BY, BETWEEN, IN, LIKE, CASE, and aggregate functions to query and analyze data from tables. Students are given tasks to write SQL queries using these functions to select, filter, sort, and calculate values from sample employee data. Examples are provided for how to eliminate duplicates, sort results, filter using ranges or lists of values, modify values conditionally, and compute totals, averages, and other aggregate statistics.

Uploaded by

Javeria Abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views6 pages

DBMS Lab 07 04112020 012905pm

The document is a lab manual for a database management systems course, containing instructions and examples for using SQL functions like DISTINCT, ORDER BY, BETWEEN, IN, LIKE, CASE, and aggregate functions to query and analyze data from tables. Students are given tasks to write SQL queries using these functions to select, filter, sort, and calculate values from sample employee data. Examples are provided for how to eliminate duplicates, sort results, filter using ranges or lists of values, modify values conditionally, and compute totals, averages, and other aggregate statistics.

Uploaded by

Javeria Abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1

Department of Computer Science


Bahria University, Islamabad

Fall 2020

Course Code : CSL-220

Database Management Systems

BS-IT-4AB

Lab Manual 7

Student Name: ________________________________

Enrollment Number: __________________________


1

Lab # 7
 DISTINCT, Order By, Between, In, Like, Case, Aggregate Functions
_________________________________________________________________________________________

Duplicate Rows
SQL Plus displays the results of a query without eliminating duplicate rows.

To eliminate duplicate rows in the result, include the DISTINCT keyword in the SELECT clause
immediately after the SELECT keyword.

ORDER BY Clause:
The SQL ORDER BY clause controls the sequence of information returned by the SELECT statement.

Why Use SQL ORDER BY?


• Use the SQL ORDER BY clause whenever it is desirable to control the sequence output through a
SELECT clause. It might be used to alphabetize a report or to print the most import items at the
beginning of a report.

• The ORDER BY clause is used to sort the tuples in a query result based on the values of some
attribute(s)
SELECT ename, 12*sal as yearSal
FROM emp
WHERE job = ‘Manager’
ORDER BY yearSal desc

1. Display the information about the managers and clerks from the column ‘Job’ in the table emp.

Order the result by deptno.

1|Page
BETWEEN , IN and LIKE:
 The BETWEEN operator allows you to formulate certain conditions a bit more easily and more
readably.

SELECT ename, sal


FROM emp WHERE
sal BETWEEN 1300 AND 2300;
 With the IN operator, you can compare a column or the outcome of a column expression against
a list of values. Using the IN operator is also a simpler way of writing a series of OR conditions.
Instead of writing empno = 7499 OR empno = 7566 OR empno = 7788, you simply use an IN-list

 You typically use the LIKE operator in the WHERE clause of your queries in combination with a
search pattern

2. List the employee names that don’t end with ‘s’.

3. List the emp names that begin with ‘c’.

4. List the names of the employees starts with T/t and ends with R/r.

5. Find the empno, job, and hire date of all Clerks and managers hired before 1990. (use IN )

CASE Expression

CASE expressions let you use IF-THEN-ELSE logic in SQL statements without having to invoke
procedures.

In a simple CASE expression, the Oracle server searches for the first WHEN ... THEN pair for which
expr

is equal to comparison_expr and returns return_expr. If none of the WHEN ... THEN pairs meet this
condition, and if an ELSE clause exists, then the Oracle server returns else_expr. Otherwise, the
Oracle server returns null. You cannot specify the literal NULL for all the return_exprs and the
else_expr.

All of the expressions ( expr, comparison_expr, and return_expr) must be of the same data type,
which can be CHAR, VARCHAR2, NCHAR, or NVARCHAR2.

2|Page
3

In the SQL statement in the slide, the value of JOB_ID is decoded. If JOB_ID is IT_PROG, the salary
increase is 10%; if JOB_ID is ST_CLERK, the salary increase is 15%; if JOB_ID is SA_REP, the salary
increase is 20%. For all other job roles, there is no increase in salary.

The same statement can be written with the DECODE function.

This is an example of a searched CASE expression. In a searched CASE expression, the search
occurs from left to right until an occurrence of the listed condition is found, and then it returns
the return expression. If no condition is found to be true, and if an ELSE clause exists, the return
expression in the ELSE clause is returned; otherwise, NULL is returned.

SELECT last_name,salary,
(CASE WHEN salary<5000 THEN 'Low'
WHEN salary<10000 THEN 'Medium'
WHEN salary<20000 THEN 'Good'
ELSE 'Excellent'
END) qualified_salary
FROM employees;

Aggregate Functions

An SQL group function or aggregate functions performs an operation on a group of rows and returns
a single result. You may want to retrieve group of item-prices and return totalprice. This type of
scenario is where you would use a group function.
To use a group function in a SQL query, list the function name followed by numeric column name
within parentheses. AVG averages the column, COUNT counts the number of items, MAX returns
maximum number of the column, and MIN returns minimum number of the column.
The following table is summary of some SQL group functions & query examples

Function Description Query Example


AVG(fieldname) Returns average value of a column SELECT avg(price)FROM inventory;
COUNT(fieldname Returns number of items in table or SELECT count(product_id)FROM
) COUNT(*) queried items product;

3|Page
SELECT count(*) FROM product;
MAX(fieldname) Returns maximum value of a column SELECT max(price)FROM inventory;
MIN(fieldname) Returns minimum value of a column SELECT min(price)FROM inventory;
SUM(fieldname) Returns total value of a column SELECT sum(price)FROM inventory;
STDDEV Returns standard deviation SELECT stddev(price)FROM inventory;
VARIANCE Returns the variance SELECT variance(price)FROM inventory;

Group Functions and Null Values


All group functions except COUNT (*) ignore null values in the column.

6. Display the total salary amount being dispersed every month of an organization assuming the
records are placed in Emp table.
7. Show the lowest and highest salary of employees.
8. Display the variation using standard deviation and variance in salaries of employees.
9. Show the total number of employees in an organization.
10. Write a query to display the total number of employees in deptno 20.
11. Display the highest, lowest & average salaries of employees who were hired in July and August
month.

4|Page
5

Lab Tasks
1. Display the information about the managers and clerks from the column ‘Job’ in the table emp, order
the result by deptno.

2. List the employee names that don’t end with ‘s’.


3. List the emp names that begin with ‘c’.
4. List the names of the employees starts with T/t and ends with R/r.
5. Find the empno, job, and hire date of all Clerks and managers hired before 1990. (use IN )
6. Display the total salary amount being dispersed every month of an organization assuming the
records are placed in Emp table.
7. Show the lowest and highest salary of employees.
8. Display the variation using standard deviation and variance in salaries of employees.
9. Show the total number of employees in an organization.
10. Write a query to display the total number of employees in deptno 20.
11. Display the highest, lowest & average salaries of employees who were hired in July and August
month.

5|Page

You might also like