6 Ex SQL I
6 Ex SQL I
You are asked to write SQL queries that answer the questions listed below. Every
student should now have an account for the PostgreSQL server at the faculty. Connect
to your PostgreSQL database from a Linux shell using the pgAdmin tool or the the
psql command shell.
Tip: You start psql from a Linux shell typing
or simply
psql -h database.inf.unibz.it hdb name i hlogin i.
Tip: If you work with psql create a file where you keep your queries and type your
queries first in an editor, then enter them into the psql command by copy and paste. If
you work with pgAdmin, you may want to save the content in the query window from
time to time into a file. In this way, you can work more efficiently.
1. What are the names of managers that have a salary between 1500 and 2500?
2. What are the names of the salesmen who have an income (= salary plus commis-
sion) above 2000? (Note that salary is never NULL, but the the commission can
be null).
3. What are the names of the employees that work in New York?
4. What is the average salary by town?
5. What are the employees that earn more than their manager?
6. Which salesmen earn a salary of grade 2?
7. What is the salary grade of each employee? (Sort the output according to the
salary grades and within each grade, according to the alphabetical order of the
names of the employees.)
1
8. What are the names of the employees who are managers of at least one salesman?
9. Which are the departments that employ someone who is not a salesman?
10. What are the names of the employees who are managers of at least three people?
11. What are the names of the employees who are not managing anyone?
12. What are the names of the employees who either have the job title manager or
who are managing other employees.
13. For how many employees is it the case that the employee and his/her manager
work in different towns?
15. For each town, find the names of the employees with the highest salary in that
town.
Choose two queries that require one or more joins and write them in two variants
• expressing the joins implicitly, using appropriate FROM and WHERE clauses.