SQL(Notes)
SQL(Notes)
1. What is Data ?
Ans
where:
2. What is Database ?
Ans
Ans
1. Create / Insert
2. Read / Retrieve
3. Update / Modify
4. Delete / Drop
Ans
1. Security
2. Authorization
SQL Notes 1
2. HDBMS (Hierarchical Data Base Management System)
Ans
1. Security
2. Authorization
Ans
7. Terminologies ?
Ans
Table
Column
Rows
Cell
Cell is the smallest unit in the table in which we store data. the intersection of
rows and column generate cells.
SQL Notes 2
Entity
Ans
“sequel language”
SQL is used for accessing, manipulating, and communicating with the database.
Almost every function such as retrieving the data from the database, creating a
new database, insertion, deletion and updating can be performed by using SQL.
What is Query ?
or
SQL needs a structure for all its objects- like table creation, procedure
creation etc. There is the common structure to create each database objects.
that is why we call it as a Structural.
or
Ans
RDBMS DBMS
SQL Notes 3
RDBMS DBMS
4) While handling data, It provides multiple 4) While handling data, It provides low
layers of Security. Security.
5) We can access Multiple data together. 5) We can access Individual data together.
10) Ex: Oracle, SQL Server. 10) Ex: XML, Microsoft Access.
Ans
1) RDBMS follows EF Codd rule (or) 1) Excel Sheet does not follows EF Codd rule
Relational Model. (or) Relational Model.
2) We can store huge amount of data. 2) We can store limited amount of data.
3) It has the features of data security. 3) It does not has the features of data security.
Ans
In RDBMS we store everything in the form of tables including META data (the
details about the data is META data)
According to the E.F Codd we can store data in multiple tables, if need we can
establish connection between two tables using key-attributes.
SQL Notes 4
We can validate the data entered into the table in two step. 1). Assigning data-
type 2).Assigning constraints.
Ans
5. Explain Data-types ?
Ans
Data types are used to determine the what kind of data we are going to store in
memory location. 5types of data-types in SQL.
char
char data type can accept character such as ‘A-B’, ’a-b’, ‘0-9’ or any special
character.
varchar/varchar2
varchar data type can accept character such as ‘A-B’, ’a-b’, ‘0-9’ or any
special character.
number
SQL Notes 5
Scale is use to determine the number of digits we are going to store in
decimal place within the precision
date
large object
Ans
Ans
Constraints are the condition that are assigned to a particular column to validate
the data.
Unique
SQL Notes 6
Not null
Check
Primary key
Foreign key
Ans
1) We can have only One Primary key in a 1) We can have n number of Foreign key in a
table. table.
2) It cannot accept repeated (or) duplicate 2) It can accept repeated (or) duplicate values
values and also Null. and also Null.
4) Primary key is not mandatory but 4) Foreign key also called as “Referential
recommended to have one in Table. Integrity Constraints”
Ans
SQL Notes 7
Check constraints is assigned with a condition, if the condition is true the value
gets accepted, else rejected.
Ans
SELECT
This statement is used to retrieve the data from database and display it.
PROJECTION
This statement is used to retrieve the data from database by selecting only
column.
SELECTION
This statement is used to retrieve the data from database by selecting both
column as well as records.
JOINS
Ans
Ans
Alias names should be in Single word or String enclosed with double quotes.
Ans
SQL Notes 8
13. Difference between Unique and Distinct clause ?
Ans
DISTINCT UNIQUE
old
Ans
Filter condition =
Ans
Arithmetic operator
+, -, *, /, %
Comparison Operator
=,≠
Relational Operator
SQL Notes 9
>, <, ≥, ≤
Logical Operator
2. OR= Binary addition, return true if any one condition satisfied true.
Concatenation Operator
||
Special Operator
IN=
IN
NOT IN
BETWEEN
NOT BETWEEN
same as BETWEEN operator but it rejects the value instead of selecting it
LIKE
used to match the pattern
NOT LIKE
IS
IS NOT
SQL Notes 10
same as IS operator but it rejects the value instead of selecting it.
Sub-Query Operator
ALL, ANY
ALL
ANY
2. It will return true only if any one of the condition at RHS is satisfied
Ans
17. Explain about Sub-Query and Difference between Single row sub-query and multi row
Sub-query ?
Ans
Inner query will execute first and generate the output. that output is input for
outer query and output of outer query is the result.
2).when ever the data to be select and condition to be executed are present in
different tables we use sub-query.
Types of Sub-query.
2).Multi-row sub-query.
SQL Notes 11
which returns more then one output.
Ans
Function is the list of instruction that are used to perform some specific task.
Function has 2 type,
User-defined function.
.
Single-row Function
It takes one input and generate one output then goes to the next input.
If we pass ‘n’ number of input to single row function, it returns ‘n’ number
of output.
Dual
UPPER
Syntax
SELECT UPPPER('STRING')
FROM DUAL;
Example
SELECT UPPPER('manu')
FROM DUAL;
----------OUTPUT--------
MANU
SQL Notes 12
LOWER
Syntax
SELECT LOWER('STRING')
FROM DUAL;
Example
SELECT LOWER('MANU')
FROM DUAL;
---------OUTPUT------------
manu
INITCAP
Syntax
Example
LENGTH
Syntax
SQL Notes 13
SELECT LENGTH('String')
FROM DUAL;
Example
SELECT LENGTH('MANU')
FROM DUAL;
--------OUTPUT----------
4
Ans
SELECT ENAME
FROM EMP
WHERE LENGTH(ENAME)=5;
REVERSE
Syntax
SELECT REVERSE('String')
FROM DUAL;
Example
SELECT REVERSE('MANU')
FROM DUAL;
-----------OUTPUT--------
UNAM
SUBSTR
SUBSTR() function used to extract the part of the String from the
given original String.
Syntax
SQL Notes 14
SELECT SUBSTR("ORIGINAL_STRING",POSITION[LENGTH])
FROM DUAL;
Example
SELECT SUBSTR("BANGALORE",1,1)
FROM DUAL; //B
SELECT SUBSTR("BANGALORE",0,0)
FROM DUAL; //
SELECT SUBSTR("BANGALORE",1,2)
FROM DUAL; //BA
SELECT SUBSTR("BANGALORE",0,2)
FROM DUAL; //BA
SELECT SUBSTR("BANGALORE",1,3)
FROM DUAL; //BAN
SELECT SUBSTR("BANGALORE",0,3)
FROM DUAL; //BAN
SELECT SUBSTR("BANGALORE",1,5)
FROM DUAL; //BANGA
SELECT SUBSTR("BANGALORE",-2,2)
FROM DUAL; //RE
SELECT SUBSTR("BANGALORE",-3,2)
FROM DUAL; //L
Ans
SELECT ENAME
FROM EMP
WHERE SUBSTR(ENAME,0,1) in ('A','E','I','O','U');
SQL Notes 15
2. WQTD Ename and job of emp’s, if the job starts with String
MAN or ends with String MAN ?
Ans
SELECT ENAME,JOB
FROM EMP
WHERE SUBSTR(JOB,1,3) IN 'MAN' OR
SUBSTR(JOB,-3,3) IN 'MAN';
Ans
SELECT LOWER(ENAME),REVERSE(JOB)
FROM EMP
WHERE LENGTH(ENAME)=6;
Ans
SELECT *
FROM EMP
WHERE SUBSTR(ENAME,1,1) IN 'A' OR
SUBSTR(ENAME,1,1) IN 'S';
Ans
SELECT ENAME
FROM EMP
WHERE SUBSTR(ENAME,1,3);
Ans
SQL Notes 16
SELECT *
FROM EMP
WHERE SUBSTR(ENAME,1,1) IN 'A';
Ans
SELECT SUBSTR(ENAME,1,1)
FROM EMP;
MOD
Syntax
SELECT column-name
FROM table-name
WHERE MOD(column-name,2)=1; --> (for Odd)
(or)
SELECT column-name
FROM table-name
WHERE MOD(column-name,2)=0; --> (for Even)
Example
Ans
SELECT *
FROM EMP
WHERE MOD(EMPNO,2)=1;
SQL Notes 17
Ans
SELECT *
FROM EMP
WHERE MOD(EMPNO,2)=0;
TO_CHAR
Syntax
TO_CHAR (date,'format-model')
Example
Ans
SELECT ENAME
FROM EMP
WHERE HIREDATE LIKE '%FEB%';
SQL Notes 18
2. WQTD Employees name, who were hired in the month FEB
Using SRF?
Ans
SELECT ENAME
FROM EMP
WHERE TO_CHAR (HIREDATE,'MON') IN 'FEB';
Ans
SELECT ENAME
FROM EMP
WHERE TO_CHAR (HIREDATE,'MON') IN ('OCT','NOV','DEC');
Ans
SELECT *
FROM EMP
WHERE TO_CHAR(HIREDATE,'DY') IN ('MON','WED');
Ans
SELECT *
FRROM EMP
WHERE TO_CHAR(HIREDATE,'YY') IN (81,83,84);
Ans
SQL Notes 19
SELECT *
FROM EMP
WHERE TO_CHAR(HIREDATE,'DD') IN (17,18,19,22);
SYSDATE / CURRENT_DATE
Syntax
SYSDATE OR CURRENT_DATE
Example
SELECT SYSDATE
FROM DUAL;
(OR)
SELECT CURRENT_DATE
FROM DUAL;
SYSTIMESTAMP
Syntax
SYSTIMESTAMP
Example
SELECT SYSTIMESTAMP
FROM DUAL;
TO DATE
Syntax
SQL Notes 20
TO_CHAR('DATE_STRING')
Example
SELECT TO_CHAR(TO_DATE('29-SEP-1999'),'DAY')
FROM DUAL;
-------------OUTPUT---------
WEDNESDAY
INSTR
Syntax
INSTR('Original-string','Sub-str',position,[Nth Occurance])
Example
// B A N A N A
// 1 2 3 4 5 6
SELECT INSTR('BANANA','A',1,1)
FROM DUAL; //2
SELECT INSTR('BANANA','A',1,2)
FROM DUAL; //4
SELECT INSTR('BANANA','A',1,3)
FROM DUAL; //6
SELECT INSTR('BANANA','A',2,1)
FROM DUAL; //2
SELECT INSTR('BANANA','A',2,2)
FROM DUAL; //4
SELECT INSTR('BANANA','AN',1,1)
FROM DUAL; //2
SELECT INSTR('BANANA','AN',1,2)
FROM DUAL; //4
SQL Notes 21
1. WQTD details of employees, if their name having character ‘A’
Using SRF ?
Ans
SELECT *
FROM EMP
WHERE INSTR(ENAME,'A',1,1)>0;
Ans
SELECT LOWER(ENAME)
FROM EMP
WHERE INSTR(ENAME,'A',1,2)>1;
It executes group-by-group.
It takes all input at once and combines it and generate one output.
1. MAX
2. MIN
3. SUM
4. AVG
5. COUNT
Ans
SQL Notes 22
We cannot use use multi-row function in where clause. because where clause
executes row-by-row where as multi-row function executes group-by-group.
Ans
Ans
Ans
In cartesian join record from table 1 will be merged with all the record
from of table 2
Inner join
Syntax
SELECT column-name1,column-name2
FROM table-name1,table-name2
WHERE <filter-codition>;
Example
SQL Notes 23
SELECT ENAME,DNAME
FROM EMP,DEPT
WHERE EMP.DEPTNO = DEPT.DEPTNO;
join condition
Syntax
table-name1.column-name = table-name2.column-name;
Example
EMP.DEPTNO = DEPT.DEPTNO;
SELECT ENAME,DNAME
FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO AND COMM IS NOT NULL AND EMP.DEPTNO IN (10,30);
WQTD Ename and Empno for all the Employees, who’s Empno are
(7839,7902) and are working in location new york ?
SELECT DNAME,EMPNO
FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO AND EMPNO IN (7839,7902) AND LOC IN 'NEW YORK';
WQTD Ename and Dname , who are earning more then smith ?
SELECT ENAME,DNAME
FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO AND SAL>(SELECT SAL
FROM EMP
WHERE ENAME IN 'SMITH');
SQL Notes 24
Outer join
We use outer join to get the matched records along with the unmatched
records.
We use left outer join to get the unmatched records of left table
along with matched records.
Syntax
SELECT column-name
FROM table-name1,table-name2
WHERE table-name1.column-name = table-name2.column-name(+);
Example
SELECT *
FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO(+);
We use right outer join to get unmatched records of right table along
with matched records.
Syntax
SELECT column-name
FROM table-name1,table-name2
WHERE table-name1.column-name(+) = table-name2.column-name;
Example
SELECT *
FROM EMP,DEPT
WHERE EMP.DEPTNO(+)=DEPT.DEPTNO(+);
SQL Notes 25
We use full outer join to get unmatched records of both tables along
with matched record.
Syntax
SELECT column-name
FROM table-name1 FULL OUTER JOIN table-name2
ON table-name1.column-name = table-name2.column-name;
Example
SELECT *
FROM EMP FULL OUTER JOIN DEPT
ON EMP.DEPTNO=DEPT.DEPTNO;
Self join
We use self join to join the same two table or table itself.
Syntax
SELECT column-name
FROM table-name1,table-name2
WHERE <filter-conition>;
Example
SELECT E1.ENAME,E2.ENAME
FROM EMP E1,EMP E2
WHERE E1.MGR = E2.EMPNO;
Natural join
If table contains similar column we get the output of inner join, if table
does not contains similar column we get the output of cartesian join.
SQL Notes 26
Why and When we use Natural Join ?
Syntax
SELECT column-name
FROM table-name1 NATURAL JOIN table-name2;
Example
SELECT *
FROM EMP NATURAL JOIN DEPT;
Ans
If we want to select a multiple column or single column from same table, then we
go with sub-query. (or) When ever the data to be select and condition to be
executed are present in different table, we use sub-query.
If we want to select a multiple column from two or more different table, we use
Join.
Ans
DDL statement is used for create, delete, alter, rename the Table in
database.
Create
Syntax
SQL Notes 27
CREATE TABLE table_name
{
COLUMN-NAME-1 DATATYPE NOT NULL/ [NULL],
COLUMN-NAME-2 DATATYPE NOT NULL/ [NULL],
COLUMN-NAME-N DATATYPE NOT NULL/ [NULL],
CONSTRAINTS constraint-ref-name UNIQUE(COLUMN-NAME),
CONSTRAINTS constraint-ref-name CHECK(CONDITION),
CONSTRAINTS constraint-ref-name PRIMARY KEY(COLUMN-NAME),
CONSTRAINTS constraint-ref-name FOREIGN KEY(COLUMN-NAME),
REFERENCES parent-table-name(COLUMN-NAME)
);
Example
Rename
Syntax
Example
Alter
Syntax
SQL Notes 28
ALTER TABLE table-name
ADD column-name DATATYPE CONSTAINTS;
Example
/*(This Query is used for only When we created a New Table and before
Inserting the records we can assign Not Null constraint there.
But if you already created a table and inserted some records,
if we want to add New column to existing table, you should assign
constraint as Null, once we assign NULL as constraint we can
update that null to any other value but remember we cannot
insert a record to that column again which stored as a Null,
we can update that record as any another value by using
by using UPDATE statement in DML)*/
Syntax
Example
Syntax
Example
SQL Notes 29
change the datatype
Syntax
Example
Syntax
Example
change constraints
Synatx
//UNIQUE
ALTER TABLE table-name
ADD CONSTRAINT constraint-ref-name UNIQUE(column-name);
//CHECK
ALTER TABLE table-name
ADD CONSTRAINT constraint-ref-name CHECK(condition);
//PRIMARY KEY
ALTER TABLE table-name
ADD CONSTRAINT constraint-ref-name PRIMARY KEY(column-name);
SQL Notes 30
//FOREIGN KEY
ALTER TABLE table-name
ADD CONSTRAINT constraint-ref-name FOREIGN KEY(column-name)
REFERENCE parent-table-name(column-name);
Example
//UNIQUE
ALTER TABLE CUSTOMER
ADD CONSTRAINT VOTERID_VO UNIQUE(VOTERID);
//CHECK
ALTER TABLE CUSTOMER
ADD CONSTRAINT VOTERID_GH CHECK(LENGTH(VOTERID)=6);
//PRIMARY KEY
(i DOM'T KNOW HOW TO CHANGE PRIMARY KEY)
//FOREIGN KEY
ALTER TABLE CUSTOMER
ADD CONSTRAINT PID_O FOREIGN KEY(PID) REFERENCES PROD(PID);
ALTER TABLE CUSTOMER
ADD CONSTRAINT PID_O FOREIGN KEY(PID) REFERENCES PROD(PID);
Truncate
Syntax
Example
Drop
Drop statement is used to delete the table along with table structure from
the Database.
Syntax
SQL Notes 31
DROP TABLE table-name;
Example
Syntax
SHOW RECYCLEBIN;
Example
SHOW RECYCLEBIN;
Syntax
Example
Syntax
Example
SQL Notes 32
PURGE TABLE CUSTOMER;
(OR)
PURGE TABLE PROD;
(OR)
PURGE TABLE MANU;
DML statement is used for insertion, deletion and update the records in
Database.
Insert
Syntax
----------------------------------
/ --> (for next record)
COMMIT; --> (for end)
Example
Note:-
Update
Syntax
SQL Notes 33
UPDATE table-name
SET column1=v1,column=v2,,,
WHERE <filter-condition>
Example
UPDATE PROD
SET PNAME='RAGNAR',PRATE=20
WHERE PID=4;
1. WQTD update the 1st employee name from prod table to ‘ramesh’ ?
Ans
UPDATE PROD
SET ENAME='RAMESH'
WHERE PID=1;
Ans
UPDATE MANU
SET SAL=SAL+1000;
Delete
Delete statement is used to delete the particular record from the table.
Syntax
DELETE
FROM table-name
WHERE <filter-condition>
Example
DELETE
FROM PROD
WHERE PID=1;
SQL Notes 34
TCL (Transection Control Language)
commit
Syntax
COMMIT;
save point
Syntax
SAVEPOINT savepoint-name;
rollback
Syntax
ROLLBACK TO save-name;
grant
Syntax
Example
SQL Notes 35
GRANT SELECT ON PROD TO HR;
revoke
revoke statement is used to take back the permission from another user.
Syntax
Example
SELECT
This statement is used to retrieve the data from database and display it.
PROJECTION
SELECTION
JOINS
Ans
SQL Notes 36
21. Explain about types of Key-attributes ?
Ans
Key-Attribute
Non Key-Attributes
Prime Key-Attributes
All the Key-Attributes except Prime key-Attribute are referred as Non Prime
key-Attributes.
Composite Key-Attributes
Super Key-Attributes
Foreign Key-Attribute
Ans
SQL Notes 37
Consider the relation ‘R’ with two attributes ‘X’ and ‘Y’. in which attribute ‘X’
determines attribute ‘Y’. In other words ‘X’ is dependent on ‘Y’ there exist a
Functional Dependency.
Ans
Anomaly= means it is a side effect which are caused during the DML operation.
28. How do you create empty table with same structure as another table ?
Ans
29. Create the following table as ‘ORDER’ by using below details. ProdID, OrderID,
Qty_sold, Price, Order_dt.
Ans
SQL Notes 38
CREATE TABLE ORDERS
(
ProdID NUMBER(4) REFERENCES products (prodid),
OrderID NUMBER(4) PRIMARY KEY,
Qty_sold NUMBER(3) CHECK (Qty_sold > 0),
Price NUMBER(8,2),
Order_dt DATE
);
13. WAQTD all the employees who are getting some comm with their designation is neither
manager nor analyst.
Ans
SELECT *
FROM EMP
WHERE COMM IS NOT NULL AND JOB NOT IN ('MANAGER','ANALYST');
14. WAQTD all the details of employees only if an employee’s manager’s manager doesn’t
have a reporting manager and he belongs to deptno 10 or 20.
Ans
SELECT *
FROM EMP E1,EMP E2,EMP E2
WHERE E1.MGR=E2.EMPNO AND E2.MGR=E3.EMPNO AND E3.MGR IS NULL AND E3.DEPTNO IN (10,20);
(or)
SELECT *
FROM EMP E1,EMP E2,EMP E3,DEPT D1,DEPT D2,DEPT D3
WHERE E1.MGR=E2.EMPNO AND E2.MGR=E3.EMPNO AND E1.DEPTNO=D1.DEPTNO
AND E2.DEPTNO=D2.DEPTNO AND E3.DEPTNO=D3.DEPTNO AND E3.MGR IS NULL
AND D3.DEPTNO IN (10,20);
15. WAQTD ename, empno and dname of the employee who are getting comm in dept 10 or
30 and empno are (7839,7902) and are working in loc new york.
Ans
SELECT E1.ENAME,E1.EMPNO,D1.DNAME
FROM EMP E1,DEPT D1
WHERE E1.DEPTNO = D1.DEPTNO AND COMM IS NOT NULL AND
E1.DEPTNO IN (10,30) AND EMPNO IN (7839,7902) AND LOC IN 'NEW YORK';
SQL Notes 39
(OR)
SELECT EMP.ENAME,EMP.EMPNO,DEPT.DNAME
FROM EMP, DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO AND COMM IS NOT NULL AND EMP.DEPTNO IN (10,30) AND
EMP.EMPNO IN (7839,7902) AND DEPT.LOC='NEW YORK';
16. List the details of the employees and hiredate who hired on a sunday in the month of
may using single row function.
Ans
SELECT *
FROM EMP
WHERE TO_CHAR(HIREDATE,'DY')='SUN' AND TO_CHAR(HIREDATE,'MON')='MAY';
17. WAQTD max salary and total salary of each department by considering all the employee
whose job is not ‘_’ character in it and total salary of department should be more than
5000 and should not be less then or equal to 9000.
Ans
SELECT MAX(SAL),SUM(SAL)
FROM EMP
WHERE JOB LIKE '%_%'
GROUP BY DEPTNO
HAVING SUM(SAL) > 5000 AND SUM(SAL) >= 9000
(OR)
SELECT MAX(SAL),SAL*12
FROM EMP
WHERE JOB NOT LIKE '%_%' AND SAL*12>5000 AND SAL*12<=9000
GROUP BY SAL*12;
Ans
SELECT E2.ENAME,E2.JOB,E2.COMM,E2.DEPTNO,E2.SAL,E2.SAL-E2.SAL*0.28
FROM EMP E1,EMP E2
WHERE E1.MGR = E2.EMPNO AND E1.DEPTNO IN (10,20,30) AND E2.HIREDATE > '31-DEC-1983'
SQL Notes 40
19. WAQTD EMP NAME AND HIREDATE, MANAGER NAME AND HIREDATE IF
MANAGER WAS HIRED BEFORE EMPLOYEE.
Ans
SELECT E1.ENAME,E1.HIREDATE,E2.ENAME,E1.HIREDATE
FROM EMP E1,EMP E2
WHERE E1.MGR = E2.EMPNO AND E2.HIREDATE < E1.HIREDATE
Ans
21. WAQTD ENAME, MANAGER'S NAME AND MANAGER'S MANAGER NAME AND WITH
THEIR LOC IF THE EMPLOYE HIRED BEFORE MARTIN AND MANAGER WORKING
IN ACCOUTING OR SALES DEPT AND MANAGER'S MANAGER EARNING SAL
MORE THAN SMITH.
Ans
SELECT E1.ENAME,E2.ENAME,E3.ENAME,D1.LOC,D2.LOC,D3.LOC
FROM EMP E1,EMP E2,EMP E3,DEPT D1,DEFT D2,DEPT D3
WHERE E1.MGR=E2.EMPNO AND E2.MGR=E3.EMPNO AND E1.DEPTNO=D1.DEPTNO AND
E2.DEFTNO=D2.DEPTNO AND E3.EMPNO=D3.DEPTNO AND
E1.HIREDATE<(SELECT HIREDATE
FROM EMP
WHERE ENAME='MARTIN') AND D2.DNAME IN ('ACCOUNTING','SALES')
AND E3.SAL>(SELECT SAL
FROM EMP
WHERE ENAME='SMITH');
22. WAQTD DETAILS OF AN EMP WHOSE NAMES STARTS WITH CHARACTER 'A' AND
'S' USING SINGLE ROW FUNCTIONS.
Ans
SQL Notes 41
SELECT *
FROM EMP
WHERE SUBSTR(ENAME,1,1) IN ('A','S');
23. WRITE A QUERY TO DISPLAY ENAME, MGR, HIREDATE, SALARY, NEW SALARY
WITH HIKE 25% FOR ALL THE EMPLOYEE. WHOSE NAME DOES NOT START WITH
A AND SORT THE RESULT IN ASCENDING ORDER WITH NEW SALARY.
Ans
24. WAQTD DNAME, LOC OF EMPLOYEES WHO IS GETTING 6th LEAST SAL.
Ans
SELECT DNAME,LOC
FROM DEPT
WHERE DEPTHO IN (SELECT DEPTNO
FROM EMP
WHERE SAL IN (SELECT SAL
FROM (SELECT ROWNUM SLNO, SAL
FROM (SELECT DISTINCT SAL
FROM EMP
ORDER BY SAL ASC))
WHERE SLNO-6));
25. WAQTD DETAILS OF THE EMPLYOEES AND THEIR MANAGER'S JOB AND
MANAGER'S MANAGER MGR ALONG WITH THEIR LOCATIONS IF EMPLOYEES
GETTING MORE THEN 2700 AND MANAGER WORKING EITHER IN DEPTNO. 10,20
AND MANAGER'S MANAGER WORKING IN IN ACCCOUNTING AND RESEARCH.
Ans
SELECT E1.*,E2.JOB,E3.MGR,D1.LOC,D2.LOC,D3.LOC
FROM EMP E1,EMP E2,EMP E3,DEPT D1,DEPT D2,DEPT D3
WHERE E1.MGR=E2.EMPNO AND E2.MGR=E3.EMPNO AND E1.DEPTNO=D1.DEPTNO AND
E2.DEPTNO=D2.DEPTNO AND E3.DEPTNO=D3.DEPINO AND E1.SAL>2700 AND
E2.DEPTNO IN (10,20) AND D3.DNAME IN ('ACCOUNTING','RESEARCH');
SQL Notes 42
28. WAQTD ALL DETAILS OF EMP ALONG WITH ANNUAL SAL, IF THEY ARE WORKING
IN DEPT 10 OR 20, HIRED IN THE MONTH OF SEPT AND EARNS MORE THAN 2000
AND WORKING AS PRESIDENT
Ans
Ans
30. WAQTD NAMES OF EMP GETTING SALARY MORE THAN SMITH IN NEWYORK
Ans
31. WAQTD EMP NAME, MANAGERS NAME AND HIS MANAGERS NAME, ALONG WITH
THEIR LOCATION IF EMP IS EARNING MORE THAN SMITH, MANAGER WORKING
IN SAME DEPT AS THAN OF JONES AND MANAGERS MANAGER WORKING IN
NEW YORK
Ans
Ans
Ans
Ans
Ans
SQL Notes 43
36. WAQTD 3RD MINIMUM SALARY
Ans
37. WAQTD ENAME, MANAGER'S NAME AND THEIR LOC IF EMP WORKING IN DEPT
10 OR 30 AND MANAGER EARNING MORE THAN FORD AND EMP WORKING IN
LOC NEW YORK OR CHICAGO
Ans
Ans
Ans
40. WAQTD NAME AND SAL OF THE EMPLOYEES EARNING MORE THAN KING IN THE
DEPT ACCOUNTING ?
Ans
41. WAQTD DETAILS OF EMP'S IF THEIR NAME STARTS WITH A' or 'S' USING SRF ?
Ans
42. WAQTD all details of emp along with annual salary. if employee is working as president
or analyst and hired in month of September ?
Ans
43. WAQTD names of employee earning salary more then smith in same department as that
of KING ?
Ans
SQL Notes 44
44. WAQTD employee names, managers names along with their location of emp is earning
more then SCOTT and manager working in DALLAS ?
Ans
Ans
Ans
Ans
Ans
49. WAQTD Ename, Sal, Deptno, Dname of the employee of their name having character ‘A’
in it ?
Ans
Ans
Ans
52. WAQTD Department name of employee who are getting salary more then KILLER ?
Ans
SQL Notes 45
53. WAQTD number of employee’s hired after 82 but they are getting some salary as
FORD’S salary ?
Ans
54. WAQTD number of employee working in each department in which there are atmost 5
employee are working ?
Ans
55. WAQTD ENAME, DNAME ONLY IF THE DNAME IS HAVING ATLEAST ONE ‘O’ OF
THE DNAME AND THE EMPLOYEE IS HAVING ‘E’ AS THE
SECOND LAST CHARACTER ?
Ans
56. WRITE A QUERY TO DISPLAY ENAME, MGR, HIREDATE, SALARY, NEW SALARY
WITH HIKE 25% FOR ALL THE EMPLOYEE WHOSE NAME DOES NOT START WITH
A AND SORT THE RESULT IN ASCENDING ORDER WITH NEW SALARY.
Ans
57. WAQTD ENAME,MANAGER'S NAME AND MANAGER'S MANAGER NAME AND WITH
THEIR LOC IF THE EMPLOYE HIRED BEFORE MARTIN AND MANAGER WORKING
IN ACCOUTING OR SALES DEPT AND MANAGER'S MANAGER EARNING SAL
MORE THAN SMITH.
Ans
58. WAQTD DNAME AND EMPNO FOR ALL THE EMPLOYEES WHO'S EMPNO ARE
(7839,7902) AND ARE WORKING IN LOC NEW YORK.
Ans
SQL Notes 46
59. DISPLAY ALL THE EMPLOYEES WHOSE NAME START WITH 'S' AND HAVING
SALARY
MORE THAN 'ALLEN' AND LESS THAN FORD.
Ans
60. WAQTD THE DNAME OF THE EMPLOYEE WHO IS GETTING 7TH MINIMUM SALARY
?
Ans
61. WAQTD ENAME, DNAME ONLY IF THE DNAME IS HAVING ATLEAST ONE ‘O’ OF
THE DNAME AND THE EMPLOYEE IS HAVING ‘E’ AS THE SECOND LAST
CHARACTER ?
Ans
62. DISPLAY ENAME, DNAME OF ALL THE EMPLOYEES WHOSE SALARY LESS THAN
AVG SAL OF DEPT 30.
Ans
Ans
64. WRITE A QUERY TO DISPLAY EMPLOYEE NAME, JOB, DNAME, LOCATION OF ALL
EMPLOYEES WHO ARE WORKING AS ACTUAL MANAGERS AND WORKS AT
CHICAGO.
Ans
SQL Notes 47
65. LIST THE DEPARTMENT NAMES IN WHICH THE EMPLOYEES ARE HIRED
BETWEEN 1ST OF JAN 1981 AND 31ST DEC 1982 WITH SALARY MORE THAN 1800.
Ans
Ans
Ans
68. DISPLAY ALL THE EMPLOYEES WHOSE NAME START WITH 'S' AND HAVING
SALARY
MORE THAN 'ALLEN' AND LESS THAN FORD.
Ans
69. DISPLAY EMPNO, ENAME, JOB, WHOSE JOB HAS 'E' IN IT AND DISPLAY EMPNO IN
DESCENDING ORDER.
Ans
Ans
Ans
SQL Notes 48
72. WAQTD Ename start with VOWELS (A.E.I.O.U) ?
Ans
73. WAQTD all the details along with annual salary of employee who are working in location
CHICAGO or NEW YORK ?
Ans
74. Create a table with name “product” having columns like (PID, Pname, Price, Discount
)and insert any 2 records using any of the syntex ?
Ans
75. WAQTD max salary of an emp working in each dept having at least 2 employees in each
dept ?
Ans
76. WAQTD Ename, manager Ename, Emp dname and managers dname where emp
working in deptno 10 and manager working in location CHICAGO ?
Ans
Ans
SQL Notes 49