SQL Commands

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 25

1.

CREATE A TABLE employees

SQL Create Table Code:


use mydatabase create table employees( empno , ename , init , job , mgr , bdate , msal , comm , deptno numeric(4) VARCHAR(8) VARCHAR(5) VARCHAR(8) Numeric(4) DATEtime numeric(6,2) numeric(6,2) numeric (2) ) ;

2.

ADDING A NEW ATTRIBUTE TO employees

SQL Insert Into:


insert into employees values(1,'Jason','N','TRAINER', 2,'1965-12-18',800 ,NULL,10);

insert into employees values(2,'Jerry', 'J', 'SALESREP',3, '1966-11-19', 1600, 300, 10);

insert into employees values(3,'Jord', 'T' , 'SALESREP',4, '1967-10-21', 1700, 500, 20);

insert into employees values(4,'Mary', 'J', 'MANAGER', 5, '1968-09-22', 1800, NULL, 20);

insert into employees values(5,'Joe', 30);

'P', 'SALESREP',6, '1969-08-23', 1900, 1400,

insert into employees values(6,'Black', 'R', 'MANAGER', 7, '1970-07-24', 2000, NULL, 30);

insert into employees values(7,'Red', 40);

'A', 'MANAGER', 8, '1971-06-25', 2100, NULL,

insert into employees values(8,'White', 'S', 'TRAINER', 9, '1972-05-26', 2200, NULL, 40);

insert into employees values(9,'Yellow', 'C', 'DIRECTOR',10, '1973-04-27', 2300, NULL, 20);

insert into employees values(10,'Pink', 'J', 'SALESREP',null,'1974-03-28', 2400, 0, 30);

3.

MODIFYING EXISTING COLUMN BY Its column width

ALTER TABLE Employee MODIFY (Salary number(11,2)); Table altered.

4.

Update Table

Once there's data in the table, we might find that there is a need to modify the data. To do so, we can use the UPDATE command. The syntax for this is UPDATE employees SET job= 'SALESREP' WHERE job = 'trainer'

5.

DROPPING A COLUMN
alter table employees drop column comm.

6.

DELETE

DELETE FROM employees WHERE ename='joe'

7.

Renaming a table

sp_rename Employees, newEmployee select * from newemployee

8.

CREATE A TABLE EMP3 FROM TABLE EMP

9. LIST THE NAMES OF CLERK WORKING IN DEPARTMENTR NO.- 20


Select ename,Job from newemployee where job='manager' and deptno=20

10.

LIST THE NAMES OF EMPLOYEE director or manager

select ename from newemployee where job='director' or job='manager'

11.

LIST THE NAME OF EMPLOYEE WHO ARE NOT MANAGER

Select ename from newemployee where job !='manager'

12. LIST THE NAME OF EMPLOYEE WHOSE EMPNO ARE 7369, 7521,7839,7934,7788.
select ename from newemployee where empno in(7369,7521,7839,7934,7788)

13. LIST THE DETAILS PF EMPLOYEE BELONGING TO DEPARTMENT No 20, 30, 40.
select ename from newemployee where deptno in(20,30,40);

14. LIST THE EMPLOYEE SALARY & NAME WHOSE SALARY IS BETWEEN 1000 AND 2000.
Select * from newemployee where msal between 1000 and 2000;

15. LIST THE DIFFERENT JOBS IN EMP TABLE.


Select job from newemployee

16. LIST THE EMPLOYEE NME WHO ARE NOT ELIGBLE FOR COMMISSION.
Select * from newemployee where commission is Null;

17. LIST THE NAME OF THE EMPLOYEE & JOB OF THE EMPLOYEE WHO DOES NOT REPORT TO ANY BODY(MGR IS NULL)
SELECT ename,job FROM newemployee WHERE mgr IS NULL

18. LIST THE EMPLOYEE WHO ARE ELIGBLE FOR COMMISSION


Select ename from newemployee where comm is not Null;

LIST THE DETAILS OF EMPLOYEE WHERE SALARY IS GREATER THAN 2000 & COMM IS NULL.
19.
select * from newemployee where msal > 2000 and comm is null;

20. LIST THE EMPLOYEE WHOSE NAME STARTS WITH j.


Select ename from newemployee where ename like 'j%'

21. LIST THE EMPLOYEE WHOSE NAME HAVE EXACTLY 5 CHARACTER 22. LIST THE EMPLOYEE, NAME, SAL IN ASCENDING ORDER OF SALARY.
Select empno,ename,msal from newemployee order by msal ASC ;

23. LIST THE EMPLOYEE & BIRTHDATE IN DESCENDING ORDER BY BIRTHDATE


SELECT ename,bdate FROM newemployee ORDER BY bdate DESC

24. LIST THE NUMBER OF EMPLOYEE WORKING IN THE COMPANY


SELECT COUNT(empno) FROM newemployee

25. LIST THE TOTAL SALARY PAYABLE TO EMPLOYEE


SELECT SUM(msal) FROM newemployee

26. LIST THE MAXIMUM SALARY OF EMPLOYEE WORKING AS A manager


SELECT MAX(msal) FROM newemployee where job='manager'

27. LIST THE AVERAGE & NO. OF EMPLOYEE WORKING IN DEPARTMENT NO. 20
SELECT AVG(msal),count(empno) FROM newemployee where deptno=20

28. LIST THE DEPARTMENT NO. & NO OF EMPLOYEES IN EACH DEPARTMENT 29. LIST THE AVERAGE SALARY FORM EACH EXCLUDING MANAGER 30. LIST THE TOTAL SALARY, MAXSAL, MINSAL, AVERAGE SALARY ACCORDING TO EMPLOYEE JOBWISE OR DEPTNO 20 HAVING AVERAGE SALARY GREATER THAN 2000.

31. CREATE A COMPOSITE INDEX ON EMP TABLE FOR THE COLUMN EMPNO & DEPTNO
CREATE NONCLUSTERED INDEX NI_empno_deptno ON newemployee(empno,deptno)

You might also like