SQL Exercises

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3
At a glance
Powered by AI
The document discusses using SQL statements to create, alter, select, update and delete data from tables in a database. Tables for employees, books and orders are created or altered and queries are written to select, group, filter and order data from these tables.

Tables for employee location and books were created or altered. The employee location table has attributes for employee id, location id, description, address and the books table had ISBN, secondary description and purchase price added.

Queries were written to select all data from customers and books tables, orders between dates, authors with last name Smith and related book data grouped by title.

SQL Exercises

Use the data model shown above and create the following SQL statements:
SQL Exercises
1. Create a new table that will be used to track employee location. The attributes
associated with the table: employee id, location id, location description, street
address, city, state, zipcode. Create a new table the attributes listed above. Please
identify the primary key. It has a relationship with employees. Provide SQL
statement below.
CREATE TABLE EMP_LOCATION (EMPLOYEEID VARCHAR(10) REFERENCES
EMPLOYEES, LOCATIONID VARCHAR(10) PRIMARY KEY, LOC_DESC

VARCHAR(25), STREET VARCHAR(15), ADDRESS VARCHAR(25), CITY


VARCHAR(15), STATE VARCHAR(10), ZIPCODE INTEGER(6));
2. Add three additional columns to the Books table. The attributes are book ISBN,
book secondary desc, and purchase price. Provide SQL statement below.
ALTER TABLE BOOKS ADD (BOOKISBN VARCHAR(20), BOOK_SEC_DESC
VARCHAR(15), PURCHASE_PRICE INTEGER(4));
3. Create a query to select all data from the customers table Provide the SQL
statement below:
SELECT * FROM CUSTOMERS;
4. Create a basic query that will extract the following information from the books,
title id, title, publisher, pubdate, edition and cost. Order by publisher. Provide the
SQL statement below:
SELECT TITLEID, TITLE, PUBLISHER, PUBDATE, EDITION, COST FROM
BOOKS ORDER BYPUBLISHER;
5. Find all find all customer orders placed between January 2014 and June 2014.
Provide the SQL statement below:
SELECT * FROM ORDERS WHERE ORDERDATE BETWEEN #01/01/2014# AND
#07/31/2014#;
6. Create a new query to display all authors with last name of Smith and all books
by this author. Show author id, author first name, author last name, title id, and
title. Group data by title id. Provide the SQL statement below:
SELECT AUTHORS.AUTHORID, AUTHORS.FIRSTNAME, AUTHORS.LASTNAME
BOOKS.TITLEID, BOOKS.TITLE FROM AUTHORS, BOOKS GROUP BY
BOOKS.TITLEID;
7. Update the new fields added to the book table for title id 123. Please provide the
SQL statement below:

UPDATE BOOKS SET BOOKISBN =134522ISUH452, BOOK_SEC_DESC


=ENGINEERING, PURCHASE_PRICE = 320 WHERE TITLEID=123;
8. Delete all orders placed before January 2011. Provide the SQL statement below.
DELETE FROM ORDERS WHERE ORDERDATE < #01/01/2011#;

You might also like