You are on page 1of 10

SHERIN SNEHA J

311119205051

Activity 3
SQL Queries

Q1. Create a table called emp with the following structure.


Name Type
---------- ----------------------
EMPNO NUMBER(6)
ENAME VARCHAR2(20)
JOB VARCHAR2(10)
DEPTNO NUMBER(3)
SAL NUMBER(7,2)
Allow NULL for all columns except ename and job.

CREATE TABLE emp


(EMPNO INT(6),
ENAME VARCHAR(20) NOT NULL,
JOB VARCHAR(10) NOT NULL,
DEPTNO INT(3),
SAL INT(72) );
DESCRIBE emp;

Q2: Add a column experience to the emp table numeric null is allowed.

ALTER TABLE emp ADD EXP int;


DESCRIBE emp;
SHERIN SNEHA J
311119205051

Q3: Modify the column width of the job field of emp table.

ALTER TABLE emp MODIFY JOB VARCHAR (20);


DESCRIBE emp;

Q4: Create dept table with the following structure.


Name Type
------------ ---------------------
DEPTNO NUMBER(2)
DNAME VARCHAR2(10)
LOC VARCHAR2(10)
Deptno as the primarykey

CREATE TABLE dept


(DEPTNO INT(2),
DNAME VARCHAR(10),
LOC VARCHAR(10),
CONSTRAINT pk_dept PRIMARY KEY (DEPTNO));
DESCRIBE dept;
SHERIN SNEHA J
311119205051

Q6: drop a column experience from the emp table.

ALTER TABLE emp DROP COLUMN EXP;


DESCRIBE emp;

Q7: Insert more than a record into emp table.

INSERT INTO emp VALUES (1,'Sherin','Admin',12,50500);


INSERT INTO emp VALUES (2,'Sneha','HR',7,45000);
INSERT INTO emp VALUES (3,'Sharwin','Analyst',11,50000);
SELECT * FROM emp;

Q8: Add a column such as Date of Joining (date as data type)

ALTER TABLE emp ADD JOINDATE date;


SHERIN SNEHA J
311119205051

Q9: Select employee name, job from the emp table.

SELECT ENAME, JOB FROM emp;

Q10: Delete only those who are working as analyst.

DELETE FROM emp WHERE JOB = 'Analyst';


SELECT * FROM emp;

Q11: List the records in the emp table orderby salary in ascending order.

SELECT * FROM emp ORDER BY SAL ASC ;


SHERIN SNEHA J
311119205051

Q12: List the records in the emp table orderby salary in descending order.

SELECT * FROM emp ORDER BY SAL DESC ;

Q13: Display only those employees whose deptno is 30.

SELECT ENAME FROM emp WHERE DEPTNO = 30;

Q14: Display all the details of the records whose employee name starts with “A”

SELECT * FROM emp WHERE ENAME LIKE 'A%';

Q15: Display all the details of the records whose employee name does not starts with “A”
SHERIN SNEHA J
311119205051

SELECT * FROM emp WHERE ENAME NOT LIKE 'A%';

Q16: Display the rows whose salary ranges from 30000 to 45000.

SELECT * FROM emp WHERE SAL BETWEEN 30000 AND 45000;

Q17: Calculate the total and average salary amount of the emp table.

SELECT AVG(SAL) , COUNT( * ) FROM emp;


SELECT SUM(SAL) , COUNT( * ) FROM emp;

Q18: Count the total records in the emp table.

SELECT COUNT( * ) as TOTAL_RECORD FROM emp;

Q19: Determine the max and min salary and rename the column as max_salary and min_salary.

SELECT MAX(SAL) as MAX_SALARY, MIN(SAL) as MIN_SALARY FROM emp;


SHERIN SNEHA J
311119205051

Q20: Find how many job titles are available in employee table

SELECT COUNT(DISTINCT JOB) FROM emp;

Q21:What is the difference between maximum and minimum salaries of employees in


the organization?

SELECT MAX(SAL) - MIN(SAL) DIFFERENCE FROM emp;

Q22: Add a default constraint to the salary and the set the default value as Rs.5000

ALTER TABLE emp MODIFY SAL INT(72) DEFAULT 5000.00;

Q23: Find the total amount of salary spent by each department using GROUP BY clause

SELECT DEPTNO, SUM(SAL) FROM emp GROUP BY DEPTNO;


SHERIN SNEHA J
311119205051

Q24: Display the department that has total salary paid for its employees more than
25000

Q25: Display the ENAME as first name from the employees


In the Employee table add a foreign key constraint (dept no) which references the dept table.

ALTER TABLE emp CHANGE ENAME FIRST_NAME VARCHAR(20);


SELECT * FROM emp;

ALTER TABLE emp ADD FOREIGN KEY (DEPTNO) REFERENCES dept(DEPTNO);


SHERIN SNEHA J
311119205051

Q26: Add a Column Phone number to the Employee table and also allow it to add only
unique values as the Phone number.

ALTER TABLE emp ADD PhoneNo VARCHAR(20) UNIQUE;


INSERT INTO emp VALUES (7,'Aishwariya','Sales',6,74000,'2010-07-08','9150522345');
INSERT INTO emp VALUES (8,'Sheethal','HR',8,84000,'2010-07-09','9150522345');

Q27: Display all employee names and salary whose salary is greater than minimum
salary of the company and job title starts with ‗A.

SELECT ENAME, SAL, JOB FROM emp


WHERE SAL > (SELECT min(SAL) FROM emp) AND job LIKE "A%";

Q28: Issue a query to find all the employees who work in the same job as SheSha.

SELECT ENAME FROM emp


WHERE JOB=(SELECT JOB FROM emp WHERE ENAME='Shesha');

Q29: Display the employee details, departments that the departments are same in both the
emp and dept.
SHERIN SNEHA J
311119205051

SELECT * FROM emp,dept WHERE emp.DEPTNO=dept.DEPTNO;

Q30: Display the employee details, departments that the departments are not same in
both the emp and dept.

SELECT * FROM emp,dept WHERE emp.DEPTNO!=dept.DEPTNO;

You might also like