You are on page 1of 36

BUCHAREST UNIVERSITY OF ECONOMIC STUDIES

FACULTY OF ECONOMIC CYBERNETICS,STATISTICS AND INFORMATICS

DATABASE PROJECT
FOOD DELIVERY

STUDENT: AURELIA-MARINA GIJU


COORDINATOR: ALEXANDRA FLOREA
1.PROJECT DESCRIPTION

The purpose of the project is to keep track of the entities involved in a food delivery firm. The
database for this firm was designed in SQL Develover and it contains fundamental notions of
SQL.
The project presents the state of the orders, the clients and the employees of the firm. Using 6
tables, I stored all the information that is needed in order for the firm to properly function.
The data in those tables includes information about the name of the customers, needed for the
safe delivery( the delivery employee can ask the name on which the order was placed in order
to be sure that it reached the right customer). It also include the phone number of each
customer in order to call if there are problems like a buzzer not working. Each employee has a
salary, a driver’s license and an assigned car in order to reach the destinations.Each order
must be placed online or by phone, must be paid cash or card and must be assigned to one
customer who ordered it and one employee who must deliver it. Also, some orders may
include discounts based on the restaurants the customer orders from or the final price of it.
Other information about the system is included also and will be shown briefly in the content
of the project.
The information about the employees (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license), the orders (order_id, order_mode, payment_mode,
customer_id, deliver_employee_id, order_final_price) and the customers(customer_id,
full_name, phone_number, address) is easily accessible and the connection between them is
facile, due to the relations created using facilities of SQL.
2.DATABASE SCHEMA
EMPLOYEES
TRANSPORTATION
Employee_id Number(3)
First_name Varchar2(40) Employee_id Number(3)
Last_name Varchar2(40)
Car_used Varchar2
Hire_date Date
Phone_numbe Varchar(40) Car_registration Varchar2
r
Salary Number(6) Year_of_purchase Date
Driver_license Varchar2(3)

ORDERS
Order_id Number(3) FOOD
Order_mode Varchar2 Order_id Number(3)
Payment_mode Varchar2 Restaurant_name Varchar2
Customer_id Number(3) Order_items Number(3)
Deliver_emp_id Number(3) Restaurant_discount Number(5)
Order_final_pric Varchar2(20)
e

ORDER_INFORMATION
Customers
Order_id Number(3) Customer_id Number(3)
First_name Varchar2
Restaurant_name Varchar2 Last_name Varchar2
Phone_numbe Varchar2
Discounts Number(5) r
Address Varchar2
3.CREATING, ALTERING AND DROPPING THE TABLES
I.CREATING TABLES
1) Creating table EMPLOYEES:
CREATE TABLE EMPLOYEES
(employee_id number(3) Primary Key,
first_name varchar2(40) Not Null,
last_name varchar2(40) Not Null,
hire_date date,
phone_number varchar2(40),
salary number(6),
driving_license varchar2(3),
CONSTRAINT driving_license_CK CHECK(driving_license IN('Yes','No'))
);
2) Creating table CUSTOMERS:
CREATE TABLE CUSTOMERS
(customer_id number(3) Primary Key,
first_name varchar2(40) NOT NULL,
last_name varchar2(40) NOT NULL,
phone_number varchar2(40) UNIQUE,
address varchar2(60)
);
3) Creating table ORDERS:
CREATE TABLE ORDERS
(order_id number(3) Primary Key,
order_mode varchar2(10),
payment_mode varchar2(4),
customer_id number(3),
deliver_emp_id number(3),
order_final_price varchar2(10),
CONSTRAINT deliver_FK Foreign Key(deliver_emp_id) REFERENCES
EMPLOYEES(employee_id),
CONSTRAINT order_price_FK Foreign Key(customer_id) REFERENCES
CUSTOMERS(customer_id)
);
4) Creating table TRANSPORT:
CREATE TABLE TRANSPORT
(employee_id number(3) UNIQUE,
car_used varchar2(20),
car_registration varchar2(20),
year_of_purchase date,
CONSTRAINT employee_id_FK Foreign Key(employee_id) REFERENCES
EMPLOYEES(employee_id)
);
5) Creating table FOOD:
CREATE TABLE FOOD
(restaurant_name varchar2(10) Primary Key,
order_id number(3),
order_items varchar2(40) Not Null,
restaurant_discount number(10),
CONSTRAINT order_id_FK Foreign Key(order_id) REFERENCES ORDERS(order_id)
);
6) Creating table ORDER_INFORMATION:
CREATE TABLE ORDER_INFORMATION
(order_id number(3),
restaurant_name varchar2(40),
discounts number(10),
CONSTRAINT order_id_2_FK Foreign Key(order_id) REFERENCES
ORDERS(order_id),
CONSTRAINT rest_name_FK Foreign Key(restaurant_name) REFERENCES
FOOD(restaurant_name)
);

SCRIPT OUTPUT:

And the tables are as follows:


Since in the altering tables part, I wont’t change any of the initial constraints, I will take
screenshots of the constraints to prove they are enabled there.

II.ALTERING THE TABLES


A.COLUMNS ALTERING
I am going to add a new column,named order_price in the table ORDER_INFORMATION.
 ALTER TABLE ORDER_INFORMATION ADD order_price varchar2(10);
I have used the DESC command (could have used DESCRIBE command too,since they do
the same thing) to see if the table was altered).

New column appeared- order_price.


 ALTER TABLE ORDER_INFORMATION MODIFY order_price char(10);

As we can see, the data type of the column order_price changed.


 ALTER TABLE ORDER_INFORMATION SET UNUSED COLUMN order_price;
As we can see, the table changed again.
 ALTER TABLE ORDER_INFORMATION DROP UNUSED COLUMN;
Since there is no available way to see unused columns in SQL Developer, I can’t really prove
that the command deleted the column, but the script ran successfully:

Since the drop unused column deletes the column order_price, set unused precedently, the
ALTER TABLE ORDER_INFORMATION DROP COLUMN order_price; won’t work
because it has nothing to delete.

B.CONSTRAINTS ALTERING
We are going to add a new constraint for the phone number in table employees,because it
must start with „07” in order to be an available number :
 ALTER TABLE EMPLOYEES ADD CONSTRAINT phone_CK
CHECK( phone_number LIKE '07%');
Now,all of the phone numbers in Employees must start with „07”.

I left the driving_license constraint in the screenshot too(the create table command worked-
proof)
 ALTER TABLE EMPLOYEES DROP CONSTRAINT driving_license_CK ;

The constraint disappeared.


 ALTER TABLE EMPLOYEES ADD CONSTRAINT driving_license_CK
CHECK(driving_license IN('Yes','No'));
The driving_license constraint was deleted when i used the DROP CONSTRAINT command,
so I put it back again, because we need to see if the employees can drive a car or not.
 ALTER TABLE EMPLOYEES DISABLE CONSTRAINT phone_CK;

As we can see, the phone_ck constraint was disabled


 ALTER TABLE EMPLOYEES ENABLE CONSTRAINT phone_CK;

C.ALTERING THE NAME OF A TABLE


In the database schema, i have a table named TRANSPORTATION, but when I created the
table I named it TRANSPORT, so let’s change that.
 ALTER TABLE TRANSPORT RENAME TO TRANSPORTATION;
As we can see in the screenshot below, now the name for the table is TRANSPORTATION.

III. DELETING AND RETRIEVING TABLES


Let’s drop the table ORDER_INFORMATION since it does not have a primary key and the
command is easier.
 DROP TABLE ORDER_INFORMATION;
Since I want it back, I will use the flashback table command:
 FLASHBACK TABLE ORDER_INFORMATION TO BEFORE DROP;
I created a new table named NO_NAME_TABLE in order to delete it permanently (can not
be retrieved):
 CREATE TABLE NO_NAME_TABLE
(a varchar2(40),
b number(30)
);
Now, we will delete it with the command:
 DROP TABLE NO_NAME_TABLE PURGE;

The table was succesfully deleted.

4.DML STATEMENTS
I.Insert command
 I inserted 10 names in the EMPLOYEES table, including my own(NOTE: more
employees may be added across the project):
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(1,’Marina’, ’Giju’, TO_DATE(’2019-
10-01’,’YYYY-MM-DD’),’0752077513’,2000,’No’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(2,’Andreea’, ’Comaneci’,
TO_DATE(’2019-11-03’,’YYYY-MM-DD’),’0754654513’,1200,’Yes’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(3,’Bianca’, ’Draghici’,
TO_DATE(’2019-11-03’,’YYYY-MM-DD’),’0752068359’,1200,’Yes’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(4,’Catalin’, ’Enescu’,
TO_DATE(’2018-12-10’,’YYYY-MM-DD’),’0761579513’,5000,’Yes’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(5,’Dorian’, ’Florescu’,
TO_DATE(’2017-07-30’,’YYYY-MM-DD’),’0722544326’,5500,’Yes’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(6,’Elena’, ’Hoblea’, TO_DATE(’2019-
01-21’,’YYYY-MM-DD’),’0743214367’,2100,’No’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(7,’Florin’, ’Ionescu’,
TO_DATE(’2019-04-18’,’YYYY-MM-DD’),’0752543213’,2000,’No’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(8,’George’, ’Jianu’, TO_DATE(’2018-
10-05’,’YYYY-MM-DD’),’076547513’,3000,’Yes’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(9,’Horia’, ’Lotvici’, TO_DATE(’2018-
07-01’,’YYYY-MM-DD’),’0752052343’,3000,’No’);
INSERT INTO EMPLOYEES (employee_id, first_name, last_name, hire_date,
phone_number, salary, driving_license) VALUES(10,’Ioana’, ’Marinescu’,
TO_DATE(’2019-10-20’,’YYYY-MM-DD’),’0735763897’,1000,’No’);
The screenshot with my name in it from the table EMPLOYEES.
 Inserting record in TRANSPORTATION table
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(1,'Skoda','B-100-MDA',TO_DATE('2017-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(2,'Fiat','B-90-GMA',TO_DATE('2017-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(3,'Toyota','B-86-KAD',TO_DATE('2018-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(4,'Ford','B-23-FGH',TO_DATE('2018-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(5,'Honda','B-73-DEF',TO_DATE('2018-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(6,'Hyundai','B-37-DSA',TO_DATE('2018-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(7,'Skoda','B-68-ERT',TO_DATE('2018-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(8,'Nissan','B-41-GBD',TO_DATE('2019-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(9,'Nissan','B-59-CHE',TO_DATE('2019-01-01','YYYY-MM-DD'));
INSERT INTO
TRANSPORTATION(employee_id,car_used,car_registration,year_of_purchase)
VALUES(10,'Nissan','B-12-SED',TO_DATE('2019-01-01','YYYY-MM-DD'));

 Costumers:
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (110,'Radu','Comarniceanu','0788954873','Bvd. Gheorghe Sincai');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (111,'Denisa','Gaman','0761548364','Bvd. Mircea Eliade');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (112,'Emma','Vasile','0772654395','Bvd. Primaverii');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (113,'Andrei','Doman','0779253756','Str. Olso');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (114,'Sebastian','Stancu','0730681426','Str. Duiosiei');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (115,'Bogdan','Leoveanu','0722857376','Str. Episcopiei');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (116,'Olga','Stan','0768345912','Bvd. Ion C. Bratianu');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (117,'Oana','Dan','0780465783','Bvd. Carol I');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (118,'Vlad','Mateescu','0748957231','Bvd. Regina Maria');
INSERT INTO CUSTOMERS(customer_id,first_name,last_name,phone_number,address)
VALUES (119,'Mihaela','Balan','0766688893','Bvd. Tineretului');

 Inserting data in Orders


INSERT INTO
orders(order_id,order_mode,payment_mode,customer_id,deliver_emp_id,order_final_price)
VALUES(100,'Online','Cash',110,1,112);
INSERT INTO
orders(order_id,order_mode,payment_mode,customer_id,deliver_emp_id,order_final_price)
VALUES(101,'Online','Card',111,2,96);
INSERT INTO
orders(order_id,order_mode,payment_mode,customer_id,deliver_emp_id,order_final_price)
VALUES(102,'Online','Cash',112,3,80);
INSERT INTO
orders(order_id,order_mode,payment_mode,customer_id,deliver_emp_id,order_final_price)
VALUES(103,'Online','Card',113,4,134);
INSERT INTO
orders(order_id,order_mode,payment_mode,customer_id,deliver_emp_id,order_final_price)
VALUES(104,'Online','Cash',114,5,100);
INSERT INTO
orders(order_id,order_mode,payment_mode,customer_id,deliver_emp_id,order_final_price)
VALUES(105,'By Phone','Card',115,6,190);
INSERT INTO
orders(order_id,order_mode,payment_mode,customer_id,deliver_emp_id,order_final_price)
VALUES(106,'By Phone','Cash',116,7,84);
INSERT INTO
orders(order_id,order_mode,payment_mode,customer_id,deliver_emp_id,order_final_price)
VALUES(107,'By Phone','Card',117,8,40);
INSERT INTO
orders(order_id,order_mode,payment_mode,customer_id,deliver_emp_id,order_final_price)
VALUES(108,'By Phone','Cash',118,9,56);
INSERT INTO
orders(order_id,order_mode,payment_mode,customer_id,deliver_emp_id,order_final_price)
VALUES(109,'By Phone','Card',119,10,80);

 Inserting data into FOOD


INSERT INTO FOOD(restaurant_name,order_id,order_items,restaurant_discount)
VALUES ('Thalia',100,'Lasagna,Soda',10);
INSERT INTO FOOD(restaurant_name,order_id,order_items,restaurant_discount)
VALUES ('Sardin',101,'Fish Dish,Soda',15);
INSERT INTO FOOD(restaurant_name,order_id,order_items,restaurant_discount)
VALUES ('Volare',102,'Pizza,Soda',10);
INSERT INTO FOOD(restaurant_name,order_id,order_items,restaurant_discount)
VALUES ('ZenSushi',103,'Sushi,Sauce',5);
INSERT INTO FOOD(restaurant_name,order_id,order_items,restaurant_discount)
VALUES ('Bellini',104,'Lasagna,Pasta,Pizza, Soda',10);
INSERT INTO FOOD(restaurant_name,order_id,order_items,restaurant_discount)
VALUES ('Madam B',105,'Pancake,Tea',20);
INSERT INTO FOOD(restaurant_name,order_id,order_items,restaurant_discount)
VALUES ('Sara',106,'Grill,Soda,Sauce',15);
INSERT INTO FOOD(restaurant_name,order_id,order_items,restaurant_discount)
VALUES ('Trattoria',107,'Lasagna,Seafood,Soup,Soda',10);
INSERT INTO FOOD(restaurant_name,order_id,order_items,restaurant_discount)
VALUES ('Dristor',108,'Special Dish,Soda',5);
INSERT INTO FOOD(restaurant_name,order_id,order_items,restaurant_discount)
VALUES ('City Grill',109,'Burger,Soup,Wine',10);

 Inserting into order_information

INSERT INTO ORDER_INFORMATION(order_id,restaurant_name)


VALUES(100,'Thalia');
INSERT INTO ORDER_INFORMATION(order_id,restaurant_name)
VALUES(101,'Sardin');
INSERT INTO ORDER_INFORMATION(order_id,restaurant_name)
VALUES(102,'Volare');
INSERT INTO ORDER_INFORMATION(order_id,restaurant_name)
VALUES(103,'ZenSushi');
INSERT INTO ORDER_INFORMATION(order_id,restaurant_name)
VALUES(104,'Bellini');
INSERT INTO ORDER_INFORMATION(order_id,restaurant_name) VALUES(105,'Madam
B');
INSERT INTO ORDER_INFORMATION(order_id,restaurant_name) VALUES(106,'Sara');
INSERT INTO ORDER_INFORMATION(order_id,restaurant_name)
VALUES(107,'Trattoria');
INSERT INTO ORDER_INFORMATION(order_id,restaurant_name)
VALUES(108,'Dristor');
INSERT INTO ORDER_INFORMATION(order_id,restaurant_name) VALUES(109,'City
Grill');

II.UPDATE COMMAND
UPDATING FINAL PRICES IN ORDERS TABLE
I want to update the final price, using the restaurant discounts from FOOD table. The
discounts represent the percentage that is reduced from the total price if the order value is
bigger than 100.
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=100)/100)*order_final_price)
WHERE order_id=100 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=100)/100)*order_final_price)
WHERE order_id=100 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=102)/100)*order_final_price)
WHERE order_id=102 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=103)/100)*order_final_price)
WHERE order_id=103 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=104)/100)*order_final_price)
WHERE order_id=104 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=105)/100)*order_final_price)
WHERE order_id=105 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=106)/100)*order_final_price)
WHERE order_id=106 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=106)/100)*order_final_price)
WHERE order_id=106 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=107)/100)*order_final_price)
WHERE order_id=107 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=108)/100)*order_final_price)
WHERE order_id=108 AND order_final_price > 100;
UPDATE ORDERS
SET order_final_price = order_final_price-(((SELECT restaurant_discount FROM FOOD
WHERE order_id=109)/100)*order_final_price)
WHERE order_id=109 AND order_final_price > 100;

As we can see, only the orders with a higher value than 100 were updated with the discounts.
III.DELETE COMMAND
A new employee inserted a record in the EMPLOYEES table by mistake, when learning how
to use the database:
 INSERT INTO EMPLOYEES(employee_id,first_name,last_name, salary) VALUES
(12,'a','b', 2000);
It needs to be deleted now:
 DELETE FROM EMPLOYEES
WHERE last_name = 'b';
4. SELECT COMMAND
A.UPDATE+SELECT
 As we are in the holiday season, the restaurants decided to a apply a discount for all
the orderes that contain sauces, removing the price of the sauce(5) from the final price
if the order final price si greater than 50(with the discount from the restaurant applied).
UPDATE ORDERS
SET order_final_price = order_final_price - 5
WHERE order_id IN (SELECT order_id FROM FOOD WHERE order_items LIKE
'%Sauce%')
AND order_final_price > 50 ;
2 rows have been updated

Values of the final price for order 103 and order 106 have been updated
 Create a new column in the table FOOD that shows the status of the
order(Delivered/Not Delivered). 4 new orders were placed, the status being NOT
DELIVERED. Add the column in order_information.
ALTER TABLE FOOD ADD status_of_the_order VARCHAR2 (15);
UPDATE FOOD
SET status_of_the_order = 'Delivered'
WHERE order_id=100;
UPDATE FOOD
SET status_of_the_order = 'Delivered'
WHERE order_id=101;
UPDATE FOOD
SET status_of_the_order = 'Delivered'
WHERE order_id=102;
UPDATE FOOD
SET status_of_the_order = 'Not Delivered'
WHERE order_id=103;
UPDATE FOOD
SET status_of_the_order = 'Delivered'
WHERE order_id=104;
UPDATE FOOD
SET status_of_the_order = 'Not Delivered'
WHERE order_id=105;
UPDATE FOOD
SET status_of_the_order = 'Delivered'
WHERE order_id=106;
UPDATE FOOD
SET status_of_the_order = 'Not Delivered'
WHERE order_id=107;
UPDATE FOOD
SET status_of_the_order = 'Delivered'
WHERE order_id=108;
UPDATE FOOD
SET status_of_the_order = 'Not Delivered'
WHERE order_id=109;
select * from food;
INSERT INTO
FOOD(restaurant_name,order_id,order_item,restaurant_discount,status_of_the_order)
VALUES ('Gastro',120,'French fries,Onion Rings,Chicken Wings',10,'Not Dellivered');
INSERT INTO
FOOD(restaurant_name,order_id,order_item,restaurant_discount,status_of_the_order)
VALUES ('Bistro',121,'Pork,Onion Rings,Sauce',15,'Not Dellivered');
INSERT INTO
FOOD(restaurant_name,order_id,order_item,restaurant_discount,status_of_the_order)
VALUES ('Sweets',122,'Pancake, Peanut Butter and Jelly',20,'Not Dellivered');
INSERT INTO
FOOD(restaurant_name,order_id,order_item,restaurant_discount,status_of_the_order)
VALUES ('McDonalds',123,'Big Tasty Menu,Sauce Tabasco',0,'Not Dellivered');
B.INSERT +SELECT
INSERT INTO ORDER_INFORMATION(order_id,restaurant_name,discounts)
SELECT order_id,restaurant_name,restaurant_discount FROM FOOD
WHERE order_id BETWEEN 119 AND 124;
select* from order_information;

 Since all the orders food,except the last 4 ones are not current (the not delivered ones
are not delivered because the client did not respond or other reasons), replace their
order_status with (null) and then create another table, named „Completed_orders”,
populated with the non-current orders.
UPDATE FOOD
SET status_of_the_order = NULL
WHERE order_id <=119;
select * FROM food;
C. CREATE+SELECT
CREATE TABLE COMPLETED_ORDERS
AS SELECT restaurant_name,order_id,order_items FROM FOOD
WHERE status_of_the_order IS NULL;
select * from completed_orders;

 Delete the non-current orders from order_information


DELETE FROM order_information
WHERE order_id != 120 AND order_id !=121 AND order_id !=122 AND order_id !=
123;
select * FROM order_information;
ANOTHER WAY TO SOLVE THIS:
D.DELETE+SELECT
DELETE FROM order_information
WHERE order_id IN (SELECT o.order_id FROM order_information o, food f WHERE
o.order_id=f.order_id AND status_of_the_order IS NULL );
select * FROM order_information;

INNER JOIN + ORDER BY


 Calculate the discounted values of the 4 current orders,withou taking into account the
sauce discount, knowing that the prices of the orders are 90,100,150,30 for order_id
120,121,122,123 and order them by the smaller price
SELECT o.order_id,order_final_price-((discounts/100)*order_final_price) final_price
FROM orders o, order_information i
WHERE o.order_id=i.order_id
ORDER BY final_price ASC;
OUTER JOIN
 Display the order_id,customer_id and status of the order of the 4 current orders along
with the order id and customer id of the non current orders(display the status of the
orders even if it is null)
SELECT o.order_id,o.customer_id,status_of_the_order FROM ORDERS o,FOOD f
WHERE o.order_id = f.order_id(+);
AVG,MAX,MIN,SUM,COUNT,HAVING
 Display the average price a customer pays for an order
SELECT AVG(order_final_price)
FROM ORDERS;
 Display the customer id of the highest paid order
SELECT customer_id
FROM ORDERS
WHERE order_final_price =(SELECT MAX(order_final_price) FROM ORDERS);

 Display the name of the restaurant with the smallest discount


SELECT restaurant_name
FROM FOOD
WHERE restaurant_discount=(SELECT MIN(restaurant_discount) FROM FOOD);

 Calculate the profit,knowing that the total cost for each order is 20% of the total price
of the order
SELECT SUM(order_final_price-((20*order_final_price)/100)) total_profit
FROM ORDERS;

 Display the employees who managed more than one order and the number of orders
they managed
SELECT COUNT(order_id) number_of_orders_managed, deliver_emp_id
FROM ORDERS
GROUP BY deliver_emp_id
HAVING COUNT(order_id) >=2 ;

TO_CHAR,EXTRACT,SUBSTR,SYSDATE
 Display the days in which the firm cars were purchased and how many of them were
purchased that day
SELECT TO_CHAR(year_of_purchase,'DD-MM-YYYY'),
COUNT(TO_CHAR( year_of_purchase,'DD-MM-YYYY'))
FROM TRANSPORTATION
GROUP BY TO_CHAR(year_of_purchase,'DD-MM-YYYY');
 No matter the position the employees have, once they have one year of employment,
their salary grows with a certain sum,no matter the rank starting this year. Display the
number of employees hired on all the months of the year in order to check how much
it costs to do that for every month
SELECT EXTRACT(Month FROM hire_date) Month, COUNT(EXTRACT(Month
FROM hire_date))
FROM EMPLOYEES
GROUP BY EXTRACT(Month FROM hire_date);
 SELECT TO_CHAR(SYSDATE, 'DD-MM-YYYY HH:MM:SS')
FROM DUAL;

DECODE,CASE,NVL
 Considering the fact that some employees don’t have a driver’s license, the boss
decided to give 20% bonus to the ones who have a driver’s license and only 10% to
ones who don’t own on on the next pay day. Display the last name of the employees
and the bonus they will recive
SELECT last_name, DECODE(driving_license,'Yes',0.2,'No',0.1)
FROM EMPLOYEES;

 Regardless of the department they work in, the employees are junior, team
manager,director. If the salary is greater or equal to 3000,, the employee is department
director. If the salary is between 1200 and 3000, the employee is team manager and if
the salary is lower or equal to 1200, the employee is a junior. Display the position of
every employee
SELECT salary, last_name,
CASE
WHEN salary >= 3000 THEN 'The employee is department director'
WHEN salary BETWEEN 1200 AND 3000 THEN 'The employee is team manager on
his department'
ELSE 'The employee is junior in his department'
END
FROM EMPLOYEES;
 Display all the non current orders as completed
SELECT order_id, NVL(status_of_the_order, 'COMPLETED')
FROM FOOD
WHERE status_of_the_order IS NULL ;

TABLE VIEW
CREATE VIEW EMPLOYEES_VIEW
AS SELECT FIRST_NAME,LAST_NAME,SALARY
FROM EMPLOYEES;
SELECT * FROM EMPLOYEES_VIEW;

You might also like