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

Advanced DBMS

The document contains a series of SQL queries designed for an Advanced Database Management Systems lab task. It includes tasks related to filtering, sorting, and retrieving employee data based on various conditions such as salary, commission, and hire dates. Each task is accompanied by specific SQL commands to achieve the desired results for the HR department's reporting needs.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Advanced DBMS

The document contains a series of SQL queries designed for an Advanced Database Management Systems lab task. It includes tasks related to filtering, sorting, and retrieving employee data based on various conditions such as salary, commission, and hire dates. Each task is accompanied by specific SQL commands to achieve the desired results for the HR department's reporting needs.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Mahnoor Zahoor (UW-23-CS-BS-107)

Advanced DBMS (CS-252)

BS-CS 4th-B

Lab Task#03

Task 1: Restricting and Sorting

i. The HR department wants to see a report of employees whose percentage of


commission is greater than 0.

select * from employees


where COMMISSION_PCT > 0;

ii. The HR department wants you to display the first_name, last_name , salary
and (salary+(salary*commission_pct)) as Net Salary of employees whose Net
Salary is in the range 10000 and 15000 and who gets atleast a percentage of
commission_pct.

select first_name,last_name,salary,(salary+(salary*commission_pct)) as net_salary


from employees
where commission_pct>0
and (salary+(salary*commission_pct)) between 10000 and 15000 ;
Mahnoor Zahoor (UW-23-CS-BS-107)

ii. The HR department wants to see the employees that do not belong to any
department specifically. You need to display the employee_id, first_name,
last_name and salary of employees whose department_id is null.

select employee_id,first_name,last_name,salary
from employees
where department_id is null;

iv. Write a query to display the name (first_name, last_name) and hire date for
all employees who were hired in 1987

select first_name,last_name,to_char(hire_date,'dd-mon-yy')as hire_date


from employees
where to_char (hire_date,'yyyy') ='1987' ;

v. Write a query to display the first_name of all employees who have both "b"
and "c" in their first name.

select first_name from employees


where lower(first_name) like '%b%c%'

vi. Write a query to display the last name, job, and salary for all employees
whose job is that of a Programmer or a Shipping Clerk, and whose salary is not
equal to $4,500, $10,000, or $15,000.

select first_name,job_id,salary
from employees
where (job_id like '%PROG' OR JOB_ID LIKE '%CLERK')
and salary not in (4500,10000,15000);

vii. Write a query to display the last name of employees whose names have
exactly 6 characters.
Mahnoor Zahoor (UW-23-CS-BS-107)

select last_name
from employees
where length(last_name)=6;

viii. Write a query to display the jobs/designations available in the employees


table. Select distinct job_id from employees;

SELECT DISTINCT job_id


FROM employees;

ix. The company wants to donate 15% salary of each employee. To see the record
write a query to display the name (first_name, last_name),salary and PF (15% of
salary) of all employees.

SELECT first_name, last_name, salary,


salary * 0.15 AS PF
FROM employees;
Mahnoor Zahoor (UW-23-CS-BS-107)

x. Write a query in SQL to display all the information of employees whose salary
is in the range of 8000 and 12000 and commission is not null or department
number is except the number 40, 120 and 70 and they have been hired before
June 5th, 1987.

SELECT * FROM employees


WHERE salary BETWEEN 8000 AND 12000
AND (commission_pct IS NOT NULL OR department_id NOT IN (40, 120, 70))
AND hire_date < TO_DATE('1987-06-05', 'YYYY-MM-DD');

xi. Write a query in SQL to display the full name (first and last), job id and date
of hire for those employees who was hired during November 5th, 2007 and July
5th, 2009.

SELECT first_name || ' ' || last_name AS full_name, job_id, hire_date


FROM employees
WHERE hire_date BETWEEN TO_DATE('05-NOV-2007', 'DD-MON-YYYY') AND
TO_DATE('05-JUL-2009', 'DD-MON-YYYY');

xiii. Write a query in Sql to display the full name (first and last name), salary,
and manager number for those employees who is not working under a manager.

SELECT first_name || ' ' || last_name AS full_name, salary, manager_id


FROM employees
WHERE manager_id IS NULL;

Task 2: Using Comparison Conditions

i. Because of budget issues, the HR department needs a report that displays


the last name and salary of employees who earn more than $12,000.

SELECT last_name, salary


FROM employees
WHERE salary > 12000;
Mahnoor Zahoor (UW-23-CS-BS-107)

ii. Create a report that displays the last name and department number for
employee number 176. Run the query.

SELECT last_name, department_id


FROM employees
WHERE employee_id = 176;

iii. The HR department needs to find high-salary and low-salary employees

SELECT last_name, salary


FROM employees
WHERE salary BETWEEN (SELECT MIN(salary) FROM employees) AND (SELECT
MAX(salary) FROM employees);

iv. The HR department want to know employee_is, last_name, job_id of


employees that work in department_id 60.

SELECT employee_id, last_name, job_id


FROM employees
WHERE department_id = 60;
Mahnoor Zahoor (UW-23-CS-BS-107)

Task 3: Character Strings and Dates

v. The HR department wants your program to give job_id, department_id


along with their last_name of all the employees whose last name is King.

SELECT job_id, department_id, last_name


FROM employees
WHERE last_name = 'King';

vi. The HR department wants you to write a query to find the employee_id,
last_name, job_id, department_id other than the department with id 90. Hint <>
is the symbol for !=.

SELECT employee_id, last_name, job_id, department_id


FROM employees
WHERE department_id <> 90;

Task 4: Using the IN Condition

i. write a report to get employee_id, last_name, salary, department_id for all


ployees of department with id 50,60, and 90.

SELECT employee_id, last_name, salary, department_id


FROM employees
WHERE department_id IN (50, 60, 90);
Mahnoor Zahoor (UW-23-CS-BS-107)

ii. Understand and tell what the query does?


SELECT last_name, job_id
FROM employees
WHERE job_id
NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP');

This query selects employees whose job_id is NOT 'IT_PROG', 'ST_CLERK', or 'SA_REP'.

iii. The HR department wants to get data (employee_id, last_name, salary,


department_id) for the employees with firstname Salim,Jabir and Manzoor.

Task 5: Using the LIKE Condition

LIKE Operator Description

WHERE CustomerName LIKE 'a%' Finds any values that start with "a"

WHERE CustomerName LIKE '%a' Finds any values that end with "a"

WHERE CustomerName LIKE '%or%' Finds any values that have "or" in
any position

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in

the second position

WHERE CustomerName LIKE


'a_%_%' Finds any values that start with "a"
and are at least 3 characters in
length

WHERE ContactName LIKE 'a%o' Finds any values that start with "a"
Mahnoor Zahoor (UW-23-CS-BS-107)

and ends with "o"

The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.
There are two wildcards often used in conjunction with the LIKE operator:
• % - The percent sign represents zero, one, or multiple characters
• The underscore represents a single character

iv. The HR needs to look for department_name, department_id, manager_id of


all the departments that exist in the location_id starting with 1 .

SELECT department_name, department_id, manager_id


FROM departments
WHERE location_id LIKE '1%';

v. The HR needs to look for department_name, department_id, manager_id of


all the departments that exist in the location_id starting with has 4 in second
location.

SELECT department_name, department_id, manager_id


FROM departments
WHERE location_id LIKE '_4%';

Task 6: Using the NULL Conditions

i. The HR wants to get rid of the missing details of managers in the


employeestable , write a query that gives you the last_name of those employees
whose manager_id is missing from the table.

SELECT last_name
FROM employees
WHERE manager_id IS NULL;
Mahnoor Zahoor (UW-23-CS-BS-107)

Task 7 Using the AND/OR Operator

ii. Understand and show what the query is doing?


SELECT employee_id, last_name, job_id, salary FROM employees
WHERE salary >=10000 AND job_id LIKE '%MAN%';

This selects employees earning at least $10,000 AND having "MAN" in their job_id.

iii. Understand and tell what the query is doing?


SELECT employee_id, last_name, job_id, salary FROM employees WHERE
salary >=10000 OR job_id LIKE '%SA%';

This selects employees earning at least $10,000 OR having "SA" in their job_id.

Rules of Precedence

i. SELECT last_name, job_id, salary


FROM employees
WHERE job_id = 'SA_REP'
OR job_id = 'AD_PRES'
AND salary > 15000;

The condition salary > 15000 applies only to 'AD_PRES', not 'SA_REP'.

ii. SELECT last_name, job_id, salary


FROM employees
WHERE (job_id = 'SA_REP'
OR job_id = 'AD_PRES')
AND salary > 15000;

Now, the salary condition applies to both job IDs.

Task 8: ORDER BY Clause Syntax

iii. The HR department wants to know the senior most employee details.

SELECT *
FROM employees
WHERE hire_date = (SELECT MIN(hire_date) FROM employees);

Task 9: Sorting by Column Alias


Mahnoor Zahoor (UW-23-CS-BS-107)

v. The HR wants to see the annual salary of all employees starting by the highest
paid employee.

SELECT employee_id, last_name, salary * 12 AS annual_salary


FROM employees
ORDER BY annual_salary DESC;

Task 10 Sorting by Multiple Columns

This sorts everything by column1 first and then by column2 whenever the
column1 fields for two rows are equal.

SELECT last_name, department_id, salary FROM employees ORDER


BY department_id, salary DESC;

vi. Explain output of following query.


SELECT last_name, department_id, salary FROM employees ORDER
BY department_id, salary DESC;

It sorts by department_id first, and within each department, employees are sorted by salary in
descending order.

You might also like