Exercise 1
Creating tables and inserting rows into the table
- Create a table called CUSTOMER with the following columns and attributes.
| COULUMN NAME | DATA TYPE | CONSTRAINTS |
|---|---|---|
| custno | int | primary key, not null, identity start with 5000 increment by 1 |
| custname | varchar(30) | not null |
| address | varchar(40) | not null |
| address1 | varchar(40) | |
| city | varchar(30) | not null |
| state | char(2) | not null, default TX |
| zip | char(10) |
- insert the following rows into the customer table.
| CUSTNO | CUSTNAME | ADDRESS | ADDRESS1 | CITY | STATE | ZIP |
|---|---|---|---|---|---|---|
| 5000 | A TO Z COMPUTERS | 800, MICROSOFT AVE | DALLAS | TX | 75240 | |
| 5001 | ZENITH COMPUTERS | 900, COMPUTERS AVE | SALEM | CA | 60589 | |
| 5002 | RONAN COMPUTERS | 777, MAIN STREET | SUITE 800 | IRVING | TX | 75555 |
| 5003 | ABC SYSTEMS | 567, PRESTON BLVD | NEW YORK | NY | 02589 |
- Create table named PRODUCT in the following structure.
| COULUMN NAME | DATA TYPE | CONSTRAINTS |
|---|---|---|
| prod_id | int | not null, primary key, identity start with 101 and increment by 1 |
| prod_name | varchar(20) | not null |
| price | money | not null, value must be greater than 0 (rule) |
| qty_avail | int | not null |
- insert the following rows into the product table.
- Create a table named invoice in the following structure
| COULUMN NAME | DATA TYPE | CONSTRAINTS |
|---|---|---|
| inv_no | integer | unique number per invoice |
| line_no | integer | unique with in the invoice number |
| inv_date | Date | |
| cust_no | integer | not null foreign key refer the value from customer table |
| prod_id | integer | not null foreign key refer the value from product table |
| qty | integer | |
| disc | integer | discount percentage |
| price | money |
- insert the following rows into the INVOICE table data
- create a table called customercopy with the same structure of customer table with out copying any rows into it using create table as select ...... statement.
- insert all the rows from customer table to customercopy table.
- Create a table called productcopy with only prod_id, prod_name, price columns and copy all the rows from product table in one create table as select ..... statement.
- create a table called invoicecopy with the following columns inv_no, customer name and total price and data from invoices and customer table using create table as select.... statement.
Result
CUSTOMER TABLE
| CUSTNO | CUSTNAME | ADDRESS | ADDRESS1 | CITY | STATE | ZIP |
|---|---|---|---|---|---|---|
| 5000 | A TO Z COMPUTERS | 800, MICROSOFT AVE | DALLAS | TX | 75240 | |
| 5001 | ZENITH COMPUTERS | 900, COMPUTERS AVE | SALEM | CA | 60589 | |
| 5002 | RONAN COMPUTERS | 777, MAIN STREET | SUITE 800 | IRVING | TX | 75555 |
| 5003 | ABC SYSTEMS | 567, PRESTON BLVD | NEW YORK | NY | 02589 |
PRODUCT TABLE
| PROD_ID | PROD_NAME | PRICE | QTY_AVAIL | REORD_LVL |
|---|---|---|---|---|
| 101 | WINDOWS | 2000 | 125 | 100 |
| 102 | WINDOWS | 95 | 80 | 200 |
| 103 | MONITORS | 200 | 49 | 25 |
| 105 | KEY BOARD | 20 | 125 | 25 |
| 106 | MS MOUSE | 10 | 200 | 100 |
| 107 | MOTHER BOARD | 125 | 45 | 20 |
INVOICE TABLE
| INV_NO | LINE_NO | INV_DATE | CUST_NO | PROD_ID | QTY | DISC |
|---|---|---|---|---|---|---|
| 1001 | 1 | 10-JAN-2000 | 5000 | 100 | 4 | 2 |
| 1001 | 2 | 10-JAN-2000 | 5000 | 101 | 5 | 1 |
| 1002 | 1 | 12-JAN-2000 | 5002 | 106 | 2 | 0 |
| 1002 | 2 | 12-JAN-2000 | 5002 | 103 | 10 | 1 |
| 1002 | 3 | 12-JAN-2000 | 5002 | 102 | 9 | 0 |
| 1003 | 1 | 13-FEB-2000 | 5003 | 101 | 10 | 2.5 |
| 1003 | 2 | 13-FEB-2000 | 5003 | 102 | 20 | 3 |
| 1005 | 1 | 14-FEB-2000 | 5000 | 106 | 9 | 1 |
| 1005 | 2 | 14-FEB-2000 | 5000 | 107 | 10 | 0 |
| 1006 | 1 | 15-MAR-2000 | 5002 | 100 | 23 | 2 |
| 1006 | 2 | 15-MAR-2000 | 5002 | 106 | 45 | 3 |
| 1007 | 1 | 13-APR-2000 | 5003 | 100 | 12 | 1 |
| 1007 | 2 | 13-APR-2000 | 5003 | 102 | 8 | 0 |
| 1007 | 3 | 13-APR-2000 | 5003 | 103 | 9 | 2 |
Exercise 2 (Simple Selects)
- Select all the rows from customer table.
- Select customer number, customer name, city from the customer table.
- Select all the information from the customer table for customer 5002.
- Select customer number and customer name who are from Texas (TX).
- Select customer address, city, state and zip who's customer number is less than 5003.
- Select the customer number, name who's customer numbers greater than 5002 and less than 5010.
- Select customer name and address who are from Dallas, Texas.
- Select customer name and city order the result set based on city in alphabatical order.
- Select customer name and city order the result set based on city in reverse alphabatical order.
- Select customer name who's name is AtoZ and city is Dallas and state is California.
Exercise 3 ( Groups and functions )
- Select invoice number, product id, price from invoice table for 1008 invoice.
- Select the count of products in invoice 1007.
- Select all the invoices in which product id 107 exists.
- Select the count of invoices for customer 5002.
- Select all the invoice numbers which has more than 10 qty sold, less than 5% discount and the final price is more than 5000.
- Select different product ids from the invoices table.
- Select all the invoice numbers for customer 5002, product 108 invoiced in march.
- Select the different invoice numbers where month of invoice dates is Jan or March or May
- Select invoices in which there are only 2 different products sold.
- Select invoices for the month of Jan and invoice total greater than 2000.
Exercise 4 (Joins)
- Select the different product id, product name for the customer 5003.
- Select the invoice number, customer name, sum of the price for every invoice for customer number 5001.
- Select customer number, customer name, product name which the customer bought.
- Select customer number, customer name and total amount for each customer.
- Select customer number, customer name product name and count of the products they bought.
- Select customer number, customer name, product name and count of products bought by each customer.
- Select customer number, name, invoice number for 'CA' state customers.
- Select customer name, different product name bought for from 'TX' state and name starts with 'A'.
- Select customers who has atleast one invoice.
- Select customers who has atleast 4 invoices.
Exercise 5 ( Selects with Group by, Exists, Joins )
-
Select all product ids and names which does not exists in the invoices table.
-
Select product id, name, total number of items sold, if there is no row for a particular product in invoices table then show the total items sold for that product as 0.
-
Select customer name, customer city, state, no of invoices, total invoices amount where customer has atleast 5 invoices and the total amount of the invoices exceeds 6000.
-
Select customer name, customer city, state where he did not have any corresponding invoice record.
-
Select product id, name and reorder information in the following format ie if the qty on hand is less than reorder level then display 'REACHED LOW' otherwise display 'ITS OK'
PRODUCT ID PRODUCT NAME REORDER INFORMATION 1001 MOUSE REACHED LOW 1002 KEY BOARD ITS OK -
Select customers who bought the produts whose price is more than 1000.
-
Display the state, number of customers , total number of invoices, total amount. for each state you should get only one row in the result so that we can see which state has more customers.
-
Select city, state, number of customers so that we will know which city has more customers.
-
Select same products bought by customer 5001 and 5002.
-
Select different products bought by customer 5002 and 5003.
Exercise 6 (Data Dictionary I)
- List all the user tables from the database.
- List all the tables created by james.
- List all the objects in the database created by james.
- List the different objects in the database.
- List all the stored procedures in the database.
- List all the system stored procedurs in the database.
- List all the rules except the ones which is created by james.
- List all the valid users in the current database.
- List all the users in the Server.
- List all the defaults in the current database.
Exercise 7 ( Data Control Language)
- Create a user named james.
- Grant Select permission on CUSTOMER table to james.
- Grant Insert permission on CUSTOMER table to james.
- Create a group called CUST_REP.
- Authorize Insert, Update, Select on EMP, DEPT, CUST, ORDER, REF_CD tables to CUST_REP group.
- Authorize Delete on ORDER table to CUST_REP group.
- Authroize CUST_REP role to james, steven, mark and william.
- Display the group names in which james is a member.
- Unauthorize the Update on CUST table from CUST_REP group.
- Display the Group name and number of users in that group respectively.