Professional Documents
Culture Documents
desc abc
How to recover delete rows in oracle(with thin 1 hour no other task should be perform)
*select * from test1 as of timestamp(systimestamp-interval '4' minute)
Then type rollback
INSERT ALL
INTO SCENARIO3(COL1,COL2,COL3)VALUES('A','B','C')
INTO SCENARIO3(COL1,COL2,COL3)VALUES('R','F','U')
INTO SCENARIO3(COL1,COL2,COL3)VALUES('A','B','C')
INTO SCENARIO3(COL1,COL2,COL3)VALUES('V','F','R')
INTO SCENARIO3(COL1,COL2,COL3)VALUES('V','F','R')
SELECT * FROM DUAL
UPDATE Table
UPDATE SCENARIO2 SET ID=30 WHERE NAME='CCC'
JOIN
Inner Join
Only fetch Matching data from both tables
SELECT * FROM STUD1 INNER JOIN STUD2 ON STUD1.DEPTNO=STUD2.DEPTNO
LEFT JOIN
RIGHT JOIN
what is sql
CREATE
ALTER
DROP
Truncate
Similar to DROP, the TRUNCATE statement is used to quickly remove all records from a table.
However, unlike DROP that completely destroys a table, TRUNCATE preserves its full structure
to be reused later.
Truncate statement syntax is:
TRUNCATE TABLE table_name;
For example:
TRUNCATE TABLE Employee;
In this example, we’re marking all the extents of the Employee table for deallocation, so they’re
considered empty for reuse.
SELECT
INSERT
Creates a record.
UPDATE
Modifies records.
DELETE
Deletes records.
GRANT
REVOKE
SQL Constraints
Means add some condition/restriction
We have to add conditions on column attribute
PRIMARY Key(unique+ Not null combination)-unique & not null value enter
FOREIGN Key-
CHECK Constraint-means example age>10 it will check each value enter by user.if less than 10 it
will reflect error
DEFAULT Constraint- if use will not put any value it will automatically take default value
What is the purpose of the NVL function?
The NVL function allows you to replace null values with a default value. If the value in the first
parameter is null, the function returns the value in the second parameter. If the first parameter
is any value other than null, it is returned unchanged.
Display the names of all the employees who are working as clerks and drawing a salary more
than 3000.
Display the employee number and name who are earning comm.
Display the employee number,ENAME,COMM and name who are earning comm.and not
display comm =o
SELECT EMPNO,ENAME,COMM FROM EMP WHERE COMM IS NOT NULL AND COMM != 0
SELF JOIN
S2 C2 2017
S1 C2 2017
[when inner query excute we get 6000,5000,3000. when inner query excute we get 3000,5000,6000., asc order
3000 is top 1]
Q-.Display the name of employees who have joined in 2016 and salary is greater than 10000?
SELECT "ENAME" FROM SCOTT."EMP" WHERE "HIREDATE" LIKE ‘2016’ AND "SAL">900
Q- Query the list of city names from A Table that do not start with vowels and end with vowels. Your
result can not contain duplicates.
Substring syntax
M -1 u-2 m-3 b-4 a-5 i-6 for back P- minus4 U- minus3 N- minus2 E- minus1
(‘a’, ‘e’, ’I’, ’o’, ’u’, ‘A’, ‘E’, ‘I’, ‘O’ , ‘U’ )
(‘a’, ‘e’, ’I’, ’o’, ’u’, ‘A’, ‘E’, ‘I’, ‘O’ , ‘U’ )
FROM EMP
GROUPBY DEPTID;
[This query 1st group all the dept id and then print dept id with max sal]
[FETCHING DATA FROM 2 TABLE, using join then writing simple highest sal query]
E.NAME AS ‘EMP’,E.SAL
FROM EMP E
INNER JOIN DEPT D [here we select requirement sal, name, dept id peform join]
WHERE(DEPTID,SAL)
IN
(SELECT DEPTID, MAX(SAL) [inner query wrote in above to find max sal dept wise]
FROM EMP
GROUPBY DEPTID);
SELECT EMAIL
FROM TABLENAME
SELECT TOP N * FROM EMP [fro top n record we use TOP N command]
Q- RETRIVE EMPFNAME & EMPLNAME IN A SINGLE COLUMN AS ‘FULLNAME’ FIRST NAME, LAST
NAME SEPARATED WITH SPACE
AS ‘FULL NAMAE’
FROM EMP ;
Q-RETRIVE EMP-POSITION ALONG WITH TOTAL SAL PAID FOR EACH OF THEM
SELECT EMP-POSITION, SUM(SAL) [SUM use for combination all sal paid fro each of them ]
FROM EMP
GROUP BY EMP-POSIYION; [we have use group by to gropu it and print each position]
FROM EMP;
[here we have to find all the rows whose duplicate are available, here we have to use COUNT, because
when ever we have to find out duplicates COUNT come to picture]
[in this table we have duplicate rows we have to print duplicate rows]
TABLE
Eid, NAME
1 abc
2 mr
1 abc
3 xyz
2 mr
O/p- 1,2
FROM EMP
FROM EMP
GROUP BY EMPID,DEPT
Q- remove duplicates
GROUP BY DEPT
HAVING COUNT(*)>1);
Q- fetch common column between 2 tables
INTERSET
UPDATE EMPLOYEE
WHERE DEPT-NAME=’ABC’;
Q-print details of employees where first name ends with ‘A’ and contains 6 alphabates
Q- print details of employees whose sal lies between 10000 and 50000
[3 ways max,limit,top]
MAX
In back end (SELECT MAX(SAL) FROM EMP) maxium sal will be selected example-7000
WHERE SAL < 70000 will executed where max sal is less than 6000 will be printed ]
2- LIMIT
[1st inner query will executed if you will not write DESC by default it will ascending order
ORDER BY will sorted in one particular order. LIMIT 2 means top 2 example 7000,6000 it will 6000
ORDER BY SAL by default ascending order become 6000,7000 LIMIT 1 means 1 will return 6000]
SELECT SAL FROM (SELECT SAL FORM EMP ORDER BY SAL DESC LIMIT 2)
3-TOP
[1st inner query executed then, 1 order by desc will executed in table and sorted in descending order
2nd the outer query excuted ,1st it select TOP1 ,then ASC order 6000,7000, it will select 6000 ]
[4 ways
SELECT DISTINCT * INTO NEWTABLE [it will select all distinct item from old table and put in new
table]
DELETE * FROM OLDTABLE[then it will delete all item from old table]
INSERT INTO OLDTABLE SELECT * FROM NEWTABLE[select item form new table and insert in old
table, now old table have distinct item]
FROM OLDTABLE
Using auto id
[INT IDENTITY (1,1) means IDENTITY (SEED,increament) seed is basically where you want to start,
I will start form 1 ,so it will increment every single time]
[we have a table with duplicate emp id ,when ALTER TABLE EMP ADD AUTOID INT IDENTITY (1,1)
we use this query it add a new column to table autoid to unique number for each row ,INT means
integer]
[2nd executed inner query , 1st it will group by all empid together example-a=1,a=2,a=3,b=4,c=5,d=6
then MIN auto id will retuent 1,4,5,6 because a will in 1 group , b will in 1 , c will in 1 group , 3
group min is 1 from every group ]
[DELETE * FROM EMP WHERE AUTO ID NOT IN it will delete all the item from table example – 2,3
and rest will return ]
FROM EMP
GROUP BY EMPID,EMP-NAME)
Q- query the name of students who score higher than 75 mark order your output last 3
characters of each name. if 2 or more students both have names ending In the same last 3
character[i.e kavya,navya,etc] secondary short them by ascending id
[substring(string,start,length)]
SELECT NAME
Q- quary an alphabetically ordered list of all names ,immediately followed by the first letter of
each profession enclosed in parameters.
Table
Name Occupation
Ram doctor
Sam police
Hari lawer
o/p- ram(d)
sam(p)
hari(l)
when we combine more columns and add neutral values to it we use CONCAT function
TABLE
Empid manid
121 123
450 676
131 450
[here we have to get the data from same table, we use self join , self join basically inner join
only ]
ON E.EMPID=M.EMPID
Q- select the job and the min and max sal for each group of rows with same job code, but only for
groups with more than one row and with a max sal greater than or equal to 27000
HAVING COUNT(X)>1 [Count is aggregate function ,having is when we use aggregate function]
AND MAX(SAL)>=27000;
TABLE
Car code
101
102
102
FROM STUDENTS
WHERE SUBJECT=’SCIENCE’
Q- query the number of occurences of each occupation .sort in ascending order and output them
in the format.
TABLE
Name occupation
Ram Actor
Hari Actor
Sam comedian
[here we have to use count, order by to sort, for combine use concat function ,lower function to
print lower case]
SELECT CONCAT (“THERE ARE A TOTAL OF”, COUNT (*),” ”, LOWER (OCCUPATION ), “S”)
FROM OCCUPATION
GROUP BY OCCUPATION