Database Exercises
SQL exercises

In this exercise, you will need the Project-database, which is described in Here . You can initialize the Project database like this:

  • Download the file project.sql
  • Execute the code from the file in your SQL-client

SELECT and WHERE / Project database
    Make Query which,...
  1. Shows all data from all fields in person-table.
  2. Shows firstnames and lastnames in person table in alphabetical order based to lastname.
  3. Shows all cities from person-table, but same code city be mentioned only once.
  4. Shows all data from person-table if the firstname is Jim.
  5. Shows all data from person-table if the city is Turku and birthyear is bigger than 1980.
  6. Shows all data from project-table if the place is NULL
  7. Shows all data from project-table if the place is NOT NULL
  8. Shows firstname, lastname and age of each person. Note you have to calculate the age based on birthyear and current year.
    WHERE LIKE
  9. Shows all data from person-table if last name begins with the letter s
  10. Shows all data from person-table if city begins with the letter t
AGGREGATE FUNCTIONS / Project database
  • The aggregate functions are: sum, avg, min, max and count (and group_concat in MySQL).
  • Remember that if, you put regular fields and aggregate functions to SELECT statement, you should allways add GROUP BY statement to your code.
  • If you have to make some selections from your data, you have to options: WHERE and HAVING. They have a different meaning: WHERE is used before grouping and HAVING is used after grouping.
    Write Queries which,
  1. Shows maximum salary and minimum salary
  2. Shows the number of rows in project table
  3. Shows average of birthyears
  4. Shows the amount of persons which lives in Turku
  5. Shows sum of salary of persons which lives in Turku

    Write Queries which will retrieve below views. Notice the column headers

  6. +----------+-----------------+
    | city     | Sum of Salaries |
    +----------+-----------------+
    | TURKU    |             127 |
    | HELSINKI |              75 |
    | TAMPERE  |              34 |
    +----------+-----------------+
    
  7. +----------+------------------+
    | city     | Average Salaries |
    +----------+------------------+
    | TURKU    |            42.33 |
    | HELSINKI |            37.50 |
    | TAMPERE  |            34.00 |
    +----------+------------------+
    
  8. +------------+----------------+
    | id_project | SUM(work_hour) |
    +------------+----------------+
    |        101 |           1300 |
    |        102 |            700 |
    |        103 |            200 |
    |        104 |            900 |
    +------------+----------------+
    
  9. The same as above, but if Sum of hour is <500, project is not included in view
    +------------+----------------+
    | id_project | SUM(work_hour) |
    +------------+----------------+
    |        101 |           1300 |
    |        102 |            700 |
    |        104 |            900 |
    +------------+----------------+
    
  10. +----------+----------------------+
    | city     | person               |
    +----------+----------------------+
    | HELSINKI | River,Simpson        |
    | TAMPERE  | Daniels              |
    | TURKU    | Morrison,Smith,Jones |
    +----------+----------------------+
    
  11. +----------+---------------------+
    | place    | project             |
    +----------+---------------------+
    | NULL     | Statistics          |
    | HELSINKI | Billing,Store       |
    | KUOPIO   | Customers           |
    | TURKU    | Bookkeeping,Selling |
    +----------+---------------------+
    
JOIN / Project database
    Make Query which,...
  1. Print the person lastnames and the amount of their working hour.
  2. Print the person lastnames and the sum of their working hour.
  3. Print the pnames of the project and the sum of their working hour.
  4. Print the person lastnames , project pnames and and the amount of their working hour.
  5. Print the pnames of the project where Morrison has been working.
  6. Print the project pname and person lastname and the amount of their working hour; the project without hour should be included (105 and 106)
SubQuery / Project database
    Make Query which,...
  1. Print the lastnames of person who live in the same city as Morrison.
  2. Print the pnames of project which lies in the same city where Morrison lives.
  3. Print the pnames of the project where Morrison has been working.
    NOTE : You will need two subqueries
    • The inner subquery returns the person_id of Morrison ( person-table )
    • The outer subquery gets the person_id of Morrison and returns the corresponding project_id's
    • The mainquery gets the project_id and returns the corresponding project pnames
    • NB! the mainquery gets several project_id values (because Morrison has been working on several project ) , so you must use the the word IN in your WHERE
Union / Project database
Note! The rows doesn't have to be exactly in the same order as they are below.
  1. Print this table
    +----------+------+
    | city     |Amount|
    +----------+------+
    | HELSINKI |   2  |
    | TAMPERE  |   1  |
    | TURKU    |   3  |
    | TOTAL    |   6  |
    +----------+------+
    
  2. Print this table
    +------------+-------------+-------------+
    | id_project | pname       | sum of hour |
    +------------+-------------+-------------+
    | 101        | Bookkeeping |        1300 |
    | 102        | Billing     |         700 |
    | 103        | Store       |         200 |
    | 104        | Selling     |         900 |
    | PROJ       | TOTAL       |        3100 |
    +------------+-------------+-------------+
    
  3. Print this table (you can do this also using SELECT IF )
    +-------------+----------+
    | pname       | place    |
    +-------------+----------+
    | Bookkeeping | TURKU    |
    | Billing     | HELSINKI |
    | Store       | HELSINKI |
    | Selling     | TURKU    |
    | Customers   | KUOPIO   |
    | Statistics  | NO PLACE |
    +-------------+----------+
    
  4. Print this table
    +----------+---------------------+
    | place    | project             |
    +----------+---------------------+
    | NULL     | Statistics          |
    | HELSINKI | Billing,Store       |
    | KUOPIO   | Customers           |
    | TURKU    | Bookkeeping,Selling |
    +----------+---------------------+
    
  5. Print this table
    +----------+---------------------+
    | place    | project             |
    +----------+---------------------+
    | HELSINKI | Billing,Store       |
    | KUOPIO   | Customers           |
    | TURKU    | Bookkeeping,Selling |
    | No Place | Statistics          |
    +----------+---------------------+
    
INSERT, UPDATE, DELETE / Project database
    Make Query which will
  1. Add below person to person table in database project
    firstname=Jim 
    lastname=Harrington
    city=HELSINKI 
    birthyear=1981 
    salary=42
    
  2. Update the city of above person to TURKU
  3. Delete above person
Procedures

Read the examples about procedures and transactions from here

  1. Make a Stored Procedure named addHours. That procedure should add a given amount of hours to given person(id_person) to given project (id_project). So, you should be able to call it like this:
    CALL addHours(x, y, z).
    Where
    • x is the id_person
    • y is the id_project
    • z is the amount of hours
    You can test your procedure with below values
    • CALL addHours(9999,101,500); -> should not success
    • CALL addHours(201,101,500); -> should success
    • CALL addHours(201,105,500); -> should success
  2. Make a Stored Procedure named addHoursNew. That procedure should work in the same ways as addHours, but if the x and y already exists in the hour-table, it will add the amount to the existing amount.
    In order to test the procedure, execute first this command:
    DELETE FROM hour where id_project='105';
    And then execute several times CALL addHoursNew(201,'105',500);
Referential Integity
  1. Create a database named integrity_example
  2. Create a table named customer with below code
    CREATE TABLE customer(
        idCustomer INT PRIMARY KEY,
        customerName VARCHAR(255)
    );
    
  3. Create a table named product with below code
    CREATE TABLE product(
        idProduct INT PRIMARY KEY,
        productName VARCHAR(50),
        customerId INT,
        CONSTRAINT customer_product
            FOREIGN KEY(customerId) REFERENCES customer(idCustomer) 
            ON DELETE RESTRICT ON UPDATE CASCADE 
    );
    
  4. Add two customers with the code
    INSERT INTO customer VALUES(1,'Jim Jones');
    INSERT INTO customer VALUES(2,'Ann Smith');
    
  5. Add two products with below code
    INSERT INTO product VALUES(1,'Mobile phone',1);
    INSERT INTO product VALUES(2,'Laptop',2);
    
    Check that you managed to add the data.
  6. Try to add a product with the code
    INSERT INTO product VALUES(3,'Laptop',3);
    
    You didn't managed to add the last product. What is the reason?
  7. Update the id of Jim Jones with below code
    UPDATE customer set idCustomer=100 WHERE idCustomer=1;
    
  8. Check the data of table customer and table product. Note the idProduct values
  9. Try to delete Jim Jones with the command
    DELETE FROM customer WHERE idCustomer=100;
    
    You didn't managed to add the last product. What is the reason?
  10. Update the referential integrity rule with below code
    ALTER TABLE product DROP CONSTRAINT customer_product;
    
    ALTER TABLE product ADD CONSTRAINT customer_product
    FOREIGN KEY(customerId) REFERENCES customer(idCustomer) 
    ON DELETE CASCADE ON UPDATE CASCADE; 
    
  11. Try again to delete Jim Jones with the code
    DELETE FROM customer WHERE idCustomer=100;
    
  12. Check the data from both tables. The product Moblie phone should have been deleted.



Toggle Menu