0% found this document useful (0 votes)
26 views2 pages

6 Ex SQL I

The document provides instructions and questions for students to write SQL queries on sample database tables. It includes 15 questions for students to answer by writing SQL queries on tables from a toy employee database. It also asks students to write two of the queries using both explicit and implicit joins.

Uploaded by

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

6 Ex SQL I

The document provides instructions and questions for students to write SQL queries on sample database tables. It includes 15 questions for students to answer by writing SQL queries on tables from a toy employee database. It also asks students to write two of the queries using both explicit and implicit joins.

Uploaded by

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

Introduction to Databases Fall-Winter 2009/10

Exercises Michail Kazimianec


Werner Nutt
Damiano Somenzi

SQL Queries (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

psql -h database.inf.unibz.it -d hdb name i -U hlogin i

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.

Querying a Company Database


On the Web page with exercises you find an SQL script named employees.sql.
Use the script to create and populate three tables of an employee toy database. The
queries refer to the tables in this database.
Tip: Check carefully what it means, according to that database, “to be a manager” and
“to be the manager of someone.”

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?

14. Who is the lowest paid employee in Dallas?

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 both explicitly

• expressing the joins implicitly, using appropriate FROM and WHERE clauses.

You might also like