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

Lab 6 - A (SQL Select Queries)

The document provides examples of SQL clauses and operators including SELECT, WHERE, GROUP BY, HAVING, ORDER BY, UNION, INTERSECT, EXCEPT, and CASE. Example queries show how to select, filter, group, and order data as well as combine result sets. The examples retrieve employee, department, and project information from various tables to demonstrate the use of these SQL elements.

Uploaded by

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

Lab 6 - A (SQL Select Queries)

The document provides examples of SQL clauses and operators including SELECT, WHERE, GROUP BY, HAVING, ORDER BY, UNION, INTERSECT, EXCEPT, and CASE. Example queries show how to select, filter, group, and order data as well as combine result sets. The examples retrieve employee, department, and project information from various tables to demonstrate the use of these SQL elements.

Uploaded by

Ali abbas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Example 1, Get the number and the last name of the employee with the lowest employee number:

SELECT emp_no, emp_lname FROM employee WHERE emp_no =

(SELECT MIN(emp_no) FROM employee);

Example 2, Get the employee number of the manager who was entered last in the works_on table:

SELECT emp_no FROM works_on WHERE enter_date =

(SELECT MAX(enter_date) FROM works_on WHERE job = 'Manager');

Example 3, Get the number of each job in all projects:

SELECT job, COUNT(*) job_count FROM works_on GROUP BY job;

HAVING CLAUSE:
The HAVING clause defines the condition that is then applied to groups of rows. Hence, this clause has
the same meaning to groups of rows that the WHERE clause has to the content of the corresponding
table.

Example 4, Get project numbers for all projects employing fewer than four persons:

SELECT project_no FROM works_on GROUP BY project_no HAVING COUNT(*) < 4;

Example 5, Group rows of the works_on table by job and eliminate those jobs that do not begin with the
letter M:

SELECT job FROM works_on GROUP BY job HAVING job LIKE 'M%';

ORDER BY Clause
The ORDER BY clause defines the particular order of the rows in the result of a query.

Example 6, Get department numbers and employee names for employees with employee numbers
< 20000, in ascending order of last and first names:

SELECT emp_fname, emp_lname, dept_no FROM employee WHERE emp_no < 20000 ORDER BY
emp_lname, emp_fname;

Example 7, for each project number, get the project number and the number of all employees, in
descending order of the employee number:

SELECT project_no, COUNT(*) emp_quantity FROM works_on GROUP BY project_no ORDER BY 2 DESC

(we can use column number instead of its name)

Set Operators:

To understand the set operators we need to add a new table, execute the following command.

SELECT emp_no, emp_fname, emp_lname, dept_no INTO employee_enh


FROM tbl_employee;
ALTER TABLE employee_enh ADD domicile CHAR(25) NULL;

After this, the Domicile column will have all NULL values now we will use an UPDATE query to update all
the values in this column.

UPDATE employee_enh SET domicile = 'San Antonio' WHERE emp_no = 25348;


UPDATE employee_enh SET domicile = 'Houston' WHERE emp_no = 10102;
UPDATE employee_enh SET domicile = 'San Antonio' WHERE emp_no = 18316;
UPDATE employee_enh SET domicile = 'Seattle' WHERE emp_no = 29346;
UPDATE employee_enh SET domicile = 'Portland' WHERE emp_no = 9031;
UPDATE employee_enh SET domicile = 'Tacoma' WHERE emp_no = 2581;
UPDATE employee_enh SET domicile = 'Houston' WHERE emp_no = 28559;

1. Union
SELECT domicile FROM employee_enh UNION SELECT location FROM department;

Two tables can be connected with the UNION operator if they are compatible with each other. This
means that both the SELECT lists must have the same number of columns, and the corresponding
columns must have compatible data types.

Example 8, Get the employee number for employees who either belong to department d1 or
entered their project before 1/1/2007, in ascending order of employee number:
SELECT emp_no FROM employee WHERE dept_no = 'd1'
UNION
SELECT emp_no FROM works_on WHERE enter_date < '01.01.2007'
ORDER BY 1;

2. INTERSECT and EXCEPT Set Operator

Example 9,

SELECT emp_no FROM employee WHERE dept_no = 'd1'

INTERSECT

SELECT emp_no FROM works_on WHERE enter_date < '01.01.2008';

Example 10,

SELECT emp_no FROM employee WHERE dept_no = 'd3'

EXCEPT

SELECT emp_no FROM works_on WHERE enter_date > '01.01.2008';

CASE Expressions:
Example 11,

SELECT project_name,

CASE

WHEN budget > 0 AND budget < 100000 THEN 1

WHEN budget >= 100000 AND budget < 200000 THEN 2

WHEN budget >= 200000 AND budget < 300000 THEN 3

ELSE 4

END budget_weight

FROM project;

Example 12,

SELECT project_name,

CASE

WHEN p1.budget < (SELECT AVG(p2.budget) FROM project p2)

THEN 'below average'


WHEN p1.budget = (SELECT AVG(p2.budget) FROM project p2)

THEN 'on average'

WHEN p1.budget > (SELECT AVG(p2.budget) FROM project p2)

THEN 'above average'

END budget_category

FROM project p1;

You might also like