0% found this document useful (0 votes)
29 views

Lab 3 - Aliases TO Truncate

The document details SQL commands for creating databases and tables, inserting sample data, and running queries. It creates an employee table, inserts employee records, links it to a department table, and performs various queries on the data.

Uploaded by

wafefoy686
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)
29 views

Lab 3 - Aliases TO Truncate

The document details SQL commands for creating databases and tables, inserting sample data, and running queries. It creates an employee table, inserts employee records, links it to a department table, and performs various queries on the data.

Uploaded by

wafefoy686
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/ 9

 Create company database

 Create table employee


CREATE TABLE Employee(
ID int NOT NULL,
NAME VARCHAR(20) NOT NULL,
AGE INT NOT NULL,
LOCATION VARCHAR(25),
SALARY DECIMAL,
PRIMARY KEY(ID));

 Insert following table

INSERT INTO employee(ID, NAME, AGE, LOCATION, SALARY) VALUES(001, 'Michael


Jordan', 49,'NYC',15500);
INSERT INTO employee(ID, NAME, AGE, LOCATION, SALARY) VALUES(002, 'Suzan Bones',
39,'Illinois',10000);
INSERT INTO employee(ID, NAME, AGE, LOCATION, SALARY) VALUES(003, 'Kevin Feige',
44,'Washington',11000);
INSERT INTO employee(ID, NAME, AGE, LOCATION, SALARY) VALUES(004, 'Tomas
Anderson', 41,'LA',9800);
INSERT INTO employee(ID, NAME, AGE, LOCATION, SALARY) VALUES(005, 'Scott Lang',
40,'Kentucky',7500);
INSERT INTO employee(ID, NAME, AGE, LOCATION, SALARY) VALUES(006, 'Gwanda
Stacey', 30,'South Africa',7000);
INSERT INTO employee(ID, NAME, AGE, LOCATION, SALARY) VALUES(007, 'Miles
Morales', 25,'Queens',6800);
INSERT INTO employee(ID, NAME, AGE, LOCATION, SALARY) VALUES(008, 'Miguel Ohara',
35,'Earth 2077',6500);
INSERT INTO employee(ID, NAME, AGE, LOCATION, SALARY) VALUES(009, 'May Parker',
55,'Earth 616',7300);
INSERT INTO employee(ID, NAME, AGE, LOCATION, SALARY) VALUES(010, 'Gellert
Grindelwald', 51,'Romania',13000);
INSERT INTO employee(ID, NAME, AGE, LOCATION, SALARY) VALUES(011, 'Lily El-Fouly',
33,'Middle East',8300);

 Describe the table

DESC employee;
 Add other data :
INSERT INTO employee(ID, NAME, AGE, `Location`, SALARY) VALUES(12, 'Ahmed Saeed',
28,'Alex',3300);
INSERT INTO employee(ID, NAME, AGE, `Location`, SALARY) VALUES(13, 'Ashraf Samy',
31,'Matrouh',3800);
INSERT INTO employee(ID, NAME, AGE, `Location`, SALARY) VALUES(14, 'Said Wael',
36,'Aswan',4800);
INSERT INTO employee(ID, NAME, AGE, `Location`, SALARY) VALUES(15, 'Alaa Ali',
42,'Cairo',5500);
INSERT INTO employee(ID, NAME, AGE, `Location`, SALARY) VALUES(16, 'Ali Samy',
43,'Cairo',5800);

 Display all data then display distinct cities


SELECT * FROM employee;
SELECT DISTINCT LOCATION FROM employee;

 Display IDs, NAMEs, & SALARYs of employees whose SALARY is greater than or equal
to 10000
SELECT ID, NAME, SALARY FROM employee WHERE SALARY>=10000;

 Display IDs, NAMEs, Ages, & SALARYs of employees whose SALARY is greater than or
equal to 10000 and age is less than 45
SELECT ID, NAME, SALARY, AGE FROM employee WHERE SALARY>=10000 AND AGE < 45;

 Display all data of employees ascending based on the salary


SELECT * FROM employee ORDER BY SALARY;

 Display all data of employees where the name starts with the litter ‘a’
SELECT * FROM employee
WHERE NAME LIKE 'a%';

 Display first 3 rows in table


SELECT * FROM employee LIMIT 3;
 Display all data of employees whose salary is greater than 5000 grouping the data based on
the names of the employees(alphabetically)

SELECT ID, NAME, Location, AGE , SALARY FROM employee


WHERE SALARY > 5000
GROUP BY NAME;

 Make an alteration in the salary column so that it doesn’t have a null value
ALTER TABLE employee
MODIFY SALARY DECIMAL (18, 2) NOT NULL;
DESC employee;

INSERT INTO employee (ID, NAME, LOCATION) VALUES (018, 'Eddie','Brooklyn')


#(salary = 0)

 Make an alteration in the salary column so that it does have by default the value 5000
ALTER TABLE employee
MODIFY SALARY DECIMAL (18, 2) DEFAULT 5000.00;

DELETE FROM employee WHERE ID = 018

INSERT INTO employee (ID, NAME, LOCATION) VALUES (018, 'Eddie','Brooklyn')


(#SALARY IS SET TO 5000)

 Make the data not repeated in the ‘age’ column


ALTER TABLE employee
MODIFY AGE INT NOT NULL UNIQUE;

INSERT INTO employee (ID, NAME, LOCATION, AGE) VALUES (019, 'Eve','USA', 35)
(error message Duplicate entry '35' for key 'AGE')

 Create table orders: (ID, DATE, AMOUNT) and foreign key (CUSTOMER_ID) related to
employee’s ID
CREATE TABLE ORDERS (
ID INT NOT NULL,
DATE DATETIME,
CUSTOMER_ID INT references employee (ID),
AMOUNT double,
PRIMARY KEY (ID));

DESC orders;
 Add column department id in ‘employee’ table and fill it with IDs : (1, 3, 5, 7, 9, 11, 13, 15,
17, 19)

ALTER TABLE employee


ADD COLUMN Dep_ID INT(3);

UPDATE employee

SET Dep_ID = 1

WHERE ID = 1;

UPDATE employee

SET Dep_ID = 3

WHERE ID = 2;

UPDATE employee

SET Dep_ID = 5

WHERE ID = 3;

UPDATE employee

SET Dep_ID = 7

WHERE ID = 4;

UPDATE employee

SET Dep_ID = 9

WHERE ID = 5;

UPDATE employee

SET Dep_ID = 11

WHERE ID = 6;

UPDATE employee
SET Dep_ID = 13

WHERE ID = 7;

UPDATE employee

SET Dep_ID = 15

WHERE ID = 8;

UPDATE employee

SET Dep_ID = 17

WHERE ID = 9;

UPDATE employee

SET Dep_ID = 19

WHERE ID = 10;

 Add the following table :

SQL commands:

CREATE TABLE department(


ID INT(3) NOT NULL,
D_ID int(3) NOT NULL REFERENCES employee(Dep_ID),
Name varchar(30) NOT NULL,
PRIMARY KEY(D_ID));
INSERT INTO department(ID, D_ID, Name) VALUES (1, 1, "Head Quarters");
INSERT INTO department(ID, D_ID, Name) VALUES (2, 3, "Human Resources");
INSERT INTO department(ID, D_ID, Name) VALUES (3, 5, "Public Relations");
INSERT INTO department(ID, D_ID, Name) VALUES (4, 7, "Fund Raising");
INSERT INTO department(ID, D_ID, Name) VALUES (5, 9, "IT");
INSERT INTO department(ID, D_ID, Name) VALUES (6, 11, "Sales");
INSERT INTO department(ID, D_ID, Name) VALUES (7, 13, "Complains & Suggestions");
INSERT INTO department(ID, D_ID, Name) VALUES (8, 15, "Customer Support");
INSERT INTO department(ID, D_ID, Name) VALUES (9, 17, "Natural Resources");
INSERT INTO department(ID, D_ID, Name) VALUES (10, 19, "Abroad Relations");

 Report a table contains data about name, salary of employees and their department’s
names and IDs :
SELECT emp.ID, emp.NAME, emp.SALARY, emp.Dep_ID, dep.Name
FROM employee AS emp, department as dep
WHERE emp.Dep_ID = dep.D_ID
 Report a table contains data about id, and name, for those whose salary is greater than or
equal to 8000 using alias to name columns of each data:

SELECT ID AS employee_ID, Name as emp_name FROM


employee
WHERE SALARY >= 8000;

 Display all data of employees who are not enrolled into a certain department :

SELECT * FROM employee WHERE dep_ID IS NULL;

 Display all data of employees who are enrolled into a certain department :

SELECT * FROM employee WHERE dep_ID IS NOT NULL;

 Alphabetically grouped, display all data of employees who are enrolled into a certain department :

SELECT Name, ID, AGE, LOCATION, SALARY FROM employee


WHERE dep_ID IS NOT null
GROUP BY Name;
 List the number of employees in each location
which is ALPHABETICALLY grouped, HAVING
only the locations with less than 5 employees
SELECT COUNT(ID), LOCATION, ID, NAME
FROM employee
GROUP BY LOCATION
HAVING COUNT(ID) < 5;

 List the number of employees in each location that is ALPHABETICALLY grouped


HAVING only the locations with less than 5 employees (SORTED from high to low in
count of numbers of employee in each location).

SELECT COUNT(ID), LOCATION, ID, NAME


FROM employee
GROUP BY LOCATION HAVING COUNT(ID) < 5
ORDER BY COUNT(ID) DESC;
 List the name of employees who are recorded in a department only, checking the
EXISTENCE of a salary greater than 7500$

SELECT NAME , Dep_ID


FROM employee
WHERE EXISTS (SELECT D_ID FROM department
WHERE D_ID = Dep_ID AND SALARY > 7500)

 Delete the entire data from the department table but keep the table intact (safe from deletion)
OR
 EMPTY the department table
TRUNCATE TABLE department

SELECT * FROM department

You might also like