0% found this document useful (0 votes)
13 views15 pages

(SQL)

Uploaded by

samrafatima0806
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views15 pages

(SQL)

Uploaded by

samrafatima0806
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

A.

Create the 3 tables

##Creating the Employee table##


CREATE TABLE Employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
gender CHAR(1),
birthday date,
salary INT,
branch_id INT,
mgr_id INT
);
desc Employee;

##Creating the Branch table##


CREATE TABLE Branch (
branch_id INT PRIMARY KEY,
branch_name VARCHAR(50),
mgr_id INT
);
desc Branch;

##Creating the Branch Supplier table##


CREATE TABLE Client (
client_id INT PRIMARY KEY,
client_name VARCHAR(50),
branch_id INT,
FOREIGN KEY (branch_id) REFERENCES Branch(branch_id)
);
desc Client;
B. Display structure of the 3 tables
C. Insert data into the 3 tables
##Inserting data into Employee table##
INSERT INTO Employee (emp_id, emp_name, gender, birthday, salary, branch_id, mgr_id)
VALUES
(101, 'Anand', 'M', '1990/06/02', 5000000, 1, NULL),
(102, 'Brijesh', 'M', '1990/07/28', 2500000, 3, 101),
(103, 'Chandni', 'F', '1992/09/03', 1500000, 1, 102),
(104, 'Dia', 'F', '1992/09/27', 1500000, 2, 102),
(105, 'Faraz', 'M', '1996/12/30', 1000000, 3, 103);

SELECT * FROM Employee;

##Inserting data into Branch table##


INSERT INTO Branch (branch_id, branch_name, mgr_id)
VALUES
(1, 'Mumbai', 101),
(2, 'Delhi', 104),
(3, 'Bangalore', 102);

SELECT * FROM Branch;

##Inserting data into Client table##


INSERT INTO Client (client_id, client_name, branch_id)
VALUES
(401, 'P', 2),
(402, 'Q', 2),
(403, 'R', 3),
(404, 'S', 3);
SELECT * FROM Client;
D. Command to display emp_id, emp_name, gender for employees
drawing salary > 15,00,000

##command to display emp_id, emp_name, gender for employees drawing salary >
15,00,000
SELECT emp_id, emp_name, gender FROM Employee
WHERE salary > 1500000;
E. Add female employee 106 (Shanti) born 03/08/93 with salary 12,00,000
in brand 2 working with manager 103

##Add female employee 106 (Shanti) born 03/08/93 with salary 12,00,000 in brand 2
working with manager 103

INSERT INTO Employee (emp_id, emp_name, gender, birthday, salary, branch_id, mgr_id)
VALUES (106, 'Shanti', 'F', '1993-08-03', 1200000, 2, 103);

SELECT * FROM Employee;


F. Write command to display data for all Female employees

##Write command to display data for all Female employees

SELECT * FROM Employee


WHERE gender = "F";
G. What is the average salary of Males?

SELECT AVG(salary) As Average_Male_Salary FROM Employee


WHERE gender = "M";
H. Comment on standard deviation of salary between Males and Females

##Comment on standard deviation of salary between Males and Females

SELECT gender, STDDEV(salary) As Std_Dev_Salary FROM Employee


GROUP BY gender;
I. Is there a difference between average salary of employees with
Manager 102 and 103? What is it?

SELECT mgr_id, AVG(salary) AS Average_Salary FROM Employee


WHERE mgr_id IN (102,103)
GROUP BY mgr_id;
J. Add data for mgr_id = 103 in branch table considering the relevant,
appropriate and logical data

INSERT INTO branch (branch_id, branch_name, mgr_id)


VALUES
(4, 'Pune', 103);

SELECT* FROM branch;


K. Change the NULL value of mgr_id to 104 for emp_id 101

Update Employee
SET mgr_id = 104
WHERE emp_id = 101;

SELECT * FROM Employee;


L. Display the data in Client table grouped by branch_id

SELECT branch_id, client_id, client_name FROM Client


GROUP BY branch_id, client_id, client_name
ORDER BY branch_id;
M. Display male employees whose salary >10,00,000

SELECT emp_id, emp_name, gender, salary FROM Employee


WHERE gender = 'M' AND salary > 1000000;
N. Do an inner join on table Employee and Branch using the relevant field

SELECT Employee.emp_id, Employee.emp_name, Employee.gender, Employee.salary,


Branch.branch_name, Branch.mgr_id
FROM Employee
INNER JOIN Branch ON Employee.branch_id = Branch.branch_id;
O. Using an appropriate Join give information of employee, their client
and respective Sales

You might also like