The document outlines SQL commands to create a database named 'sqlTest' and define two tables: 'Employee' and 'department'. It includes various SQL operations such as inserting records, updating data, selecting information, and creating stored procedures and views. Additionally, it demonstrates how to calculate employee ages and perform aggregations on salaries.
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 ratings0% found this document useful (0 votes)
10 views3 pages
Vidhyacharan DBSQLTEST 1310
The document outlines SQL commands to create a database named 'sqlTest' and define two tables: 'Employee' and 'department'. It includes various SQL operations such as inserting records, updating data, selecting information, and creating stored procedures and views. Additionally, it demonstrates how to calculate employee ages and perform aggregations on salaries.
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/ 3
SQL test
CREATE DATABASE sqlTest
USE sqlTest CREATE TABLE Employee( sl INT unique , Empcode INT primary key, FirstName varchar(50), LastName varchar(50), Role varchar(50) , manager INT, hiredate DATE, salary decimal(10,2), commission INT, Depcod INT, ) INSERT INTO Employee values (1,9369,'Tony','STARK','SOFTWARE ENGINNER',7902,'12/17/1980',2800,0,20) INSERT INTO Employee values (2,9499,'TIM','STARK','SALESMANE',7698,'02/20/1981',1600,300,30) INSERT INTO Employee values (3,9566,'KIM','JARVIS','MANAGER',7839,'04/02/1981',3570,0,20) INSERT INTO Employee values (4,9591,'WENDY','SAWAN','SALESMANE',7698,'02/22/1981',500,0,30) INSERT INTO Employee values (5,9654,'SAM','MILES','SALESMAN',7698,'09/28/1981',1250,1400,30) ---UPDATE-- INSERT INTO Employee values (6,9698,'BELLST','SWAN','MANAGER',7839,'05/01/1981',3420,0,30) INSERT INTO Employee values (7,9777,'MADII','HIMBURY','ANALYST',7839,'05/01/1981',2000,200,20) INSERT INTO Employee values (8,9782,'KEVIN','HILL','MANAGER',7839,'06/09/1981',2940,0,10) INSERT INTO Employee values (9,9788,'CONNIE','SMITA','ANALYSI',7566,'12/09/1982',3000,0,20) INSERT INTO Employee values (10,9839,'ALFRED','KINSLEY','PRESIDENT',7566,'11/17/1981',5000,0,10) INSERT INTO Employee values (11,99844,'PAUL','TIMOTHY','SALESMAN',7698,'09/08/1981',1500,0,30) INSERT INTO Employee values (12,9860,'ATHENA','WILSON','ANALYST',7839,'06/21/1992',7000,100,50) INSERT INTO Employee values (13,9861,'JENNIFER','HUETTE','ANALYST',7839,'07/01/1996',5000,100,50) INSERT INTO Employee values (14,9876,'JOHAN','ASGHAR','SOFTWARE ENGINNER',7788,'01/12/1983',3100,0,20) INSERT INTO Employee values (15,9900,'ROSE','SUMMERS','TECHNICAL LEAD',7698,'12/03/1981',2950,0,20) INSERT INTO Employee values (16,9902,'ANDREW','FAULKNER','ANALYST',7566,'12/03/1981',3000,0,10) INSERT INTO Employee values (17,9934,'KAREN','MATTHEWS','SOFTWARE ENGINNER',7,'01/23/1982',3300,0,20) SELECT* FROM Employee CREATE TABLE department ( sl int foreign key references employee(sl) , deptcode int primary key, deptName varchar(40), Location varchar(100) ) insert into department values(1,10,'FINANCE','EDINBURGH') insert into department values(2,20,'SOFTWARE','PADDINGTON') insert into department values(3,30,'SALES','MAIDSTONE') insert into department values(4,40,'MARKETING','EDINBURGH') insert into department values(5,50,'ADMIN','BIRMINGHAM') SELECT * FROM department --q3) select e.firstname,e.lastname,e.Depcod,d.deptname,d.location from Employee e join department As d on e.Depcod=d.deptcode order by e.FirstName Asc; --q4)--- select salary from Employee --q5) SELECT MAX(salary) AS "MAX SALARY", (SELECT TOP 1 Salary FROM ( SELECT DISTINCT TOP 2 Salary FROM employee ORDER BY Salary DESC ) AS SubQuery --q6) select SUM(Salary + Commission) AS "TOTAL SALARY" FROM employee WHERE Role = 'ANALYST' AND Depcod = 20; --q7) SELECT Role, AVG(Salary) AS "AVG SALARY", MIN(Salary) AS "MIN SALARY", MAX(Salary) AS "MAX SALARY" FROM employee WHERE Role = 'ANALYST' GROUP BY Role --q8) select *from department where Location='edinburgh' --q9) SELECT * FROM department WHERE deptName='finance' --Q10) SELECT AVG (SALARY)FROM Employee --q11) select top 10*from Employee order by salary desc -- q12) select * from Employee where commission=0 or commission=null --q13) CREATE PROCEDURE GetEmployeeDetails AS BEGIN SELECT e.EmpCode, e.FirstName, e.LastName, e.Role, d.DeptName, d.Location FROM employee e JOIN department d ON DeptCode = DeptCode; END EXEC GetEmployeeDetails --q14) CREATE PROCEDURE GetEmployeesByDeptCodes AS BEGIN SELECT e.EmpCode, e.FirstName, e.LastName, e.Role, d.DeptName, d.Location FROM employee e JOIN department d ON DeptCode =DeptCode WHERE DeptCode IN (20, 30); END; EXEC GetEmployeesByDeptCodes --q15)--- CREATE PROCEDURE GetEmployeeYearsOfService AS BEGIN SELECT EmpCode, FirstName, LastName, Role, DATEDIFF(YEAR, HireDate, GETDATE()) AS YearsOfService FROM employee; END; EXEC GetEmployeeYearsOfService --q16) CREATE VIEW EmpLocation AS SELECT FirstName, LastName, Role, Location FROM employee e JOIN department d ON DeptCode = DeptCode; ---q17) create view sam1 as select firstName from Employee where firstname='sam' select * from sam1 ---q18) create index ix_empcode on employee(empcode) ---q19) Alter table employee add DoB date update Employee set DOb='2002-08-12' where sl=1 update Employee set DOb='2005-02-12' where sl=2 update Employee set DOb='2004-08-16' where sl=3 update Employee set DOb='2008-07-11' where sl=4 update Employee set DOb='2006-08-13' where sl=5 update Employee set DOb='2009-07-19' where sl=6 update Employee set DOb='2003-04-12' where sl=7 update Employee set DOb='2002-08-11' where sl=8 update Employee set DOb='2003-05-10' where sl=9 update Employee set DOb='2004-08-12' where sl=10 update Employee set DOb='2005-06-11' where sl=1 update Employee set DOb='2009-04-06' where sl=1 update Employee set DOb='2004-07-1-07' where sl=1 update Employee set DOb='2007-05-03' where sl=1 update Employee set DOb='2003-08-05' where sl=1 update Employee set DOb='2009-02-10' where sl=1 update Employee set DOb='2002-03-11' where sl=1 --q20) SELECT EmpCode, FirstName, LastName, DATEDIFF(YEAR, DOb, GETDATE()) - CASE WHEN DATEADD(YEAR, DATEDIFF(YEAR, DOb, GETDATE()), DOb) > GETDATE() THEN 1 ELSE 0 END AS Age FROM employee select * from Employee