SQL Commands
SQL Commands
SQL Commands
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);
DESC employee;
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 where the name starts with the litter ‘a’:
SELECT * FROM employee
WHERE NAME LIKE 'a%';
• Display all data of employees whose salary is greater than 3500 grouping the data based on the names of
the employees(alphabetically):
• 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, 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):
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));
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…