0% found this document useful (0 votes)
21 views7 pages

SQL Commands

The document describes creating and populating database tables for employees and departments. It creates an "employee" table with various fields, inserts sample data for 11 employees, then adds additional employees. It also creates an "orders" table with a foreign key to employees, and a "department" table. Various queries are performed on the data, such as selecting employees by salary, filtering by name or department, and joining tables to retrieve related data. The document outlines setting up the database schema and populating it with sample data and relationships between tables.

Uploaded by

Amr Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
21 views7 pages

SQL Commands

The document describes creating and populating database tables for employees and departments. It creates an "employee" table with various fields, inserts sample data for 11 employees, then adds additional employees. It also creates an "orders" table with a foreign key to employees, and a "department" table. Various queries are performed on the data, such as selecting employees by salary, filtering by name or department, and joining tables to retrieve related data. The document outlines setting up the database schema and populating it with sample data and relationships between tables.

Uploaded by

Amr Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Database Lap

• 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(17, '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 3500:
SELECT ID, NAME, SALARY FROM employee WHERE SALARY>=3500;

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

• 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 3500 grouping the data based on the names of
the employees(alphabetically):

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


WHERE SALARY > 3500
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;

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));

• 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:
CREATE TABLE department(
D_ID int(3) NOT NULL,
Name varchar(30) NOT NULL,
PRIMARY KEY(D_ID));

INSERT INTO department(D_ID, Name) VALUES ( 1, "Head Quarters");


INSERT INTO department(D_ID, Name) VALUES ( 3, "Human Resources");
INSERT INTO department(D_ID, Name) VALUES ( 5, "Public Relations");
INSERT INTO department(D_ID, Name) VALUES ( 7, "Fund Raising");
INSERT INTO department(D_ID, Name) VALUES ( 9, "IT");
INSERT INTO department(D_ID, Name) VALUES ( 11, "Sales");
INSERT INTO department(D_ID, Name) VALUES ( 13, "Complains & Suggestions");
INSERT INTO department(D_ID, Name) VALUES ( 15, "Customer Support");
INSERT INTO department(D_ID, Name) VALUES ( 17, "Natural Resources");
INSERT INTO department(D_ID, Name) VALUES ( 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:
SELECT ID AS employee_ID, Name as emp_name FROM employee
WHERE SALARY >= 8000;

• Display all data from table employee regarding employees who are not in a certain department :
SELECT * FROM employee WHERE dep_ID IS NULL;
• Display all data from table employee regarding employees who are part of a certain department :
SELECT * FROM employee WHERE dep_ID IS NOT NULL;

• Alphabetically grouped, display all data of employees who are part of a certain department :
SELECT Name, ID, AGE, LOCATION, SALARY FROM employee
WHERE dep_ID IS NOT null
GROUP BY Name;

To be continued…

You might also like