Lab 6 - A (SQL Select Queries)
Lab 6 - A (SQL Select Queries)
Example 2, Get the employee number of the manager who was entered last in the works_on table:
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:
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
Set Operators:
To understand the set operators we need to add a new table, execute the following command.
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.
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;
Example 9,
INTERSECT
Example 10,
EXCEPT
CASE Expressions:
Example 11,
SELECT project_name,
CASE
ELSE 4
END budget_weight
FROM project;
Example 12,
SELECT project_name,
CASE
END budget_category