3.how Can I Retrive All Records of Emp1 Those Should Not Present in Emp2?

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

1.How to delete duplicate rows in a table?

1st method-delete from emp a where rowid != (select max(rowid) from emp b
where a.empno=b.empno);

2nd Mehod-delete from EmpDup where


EmpID in(select EmpID from EmpDup group by EmpId having count(*) >1)

2.To select ALTERNATE records from a table. (ODD NUMBERED)

select * from (select empno, ename, sal, rownum rn


from emp order by empno)
where mod (rn, 2) <> 0;

3.How can I retrive all records of emp1 those should not present in emp2?

(Select * from emp) Minus (Select * from emp1)

4. Count of number of employees in department wise.

select count(EMPNO), b.deptno, dname from emp a, dept b where a.deptno=b.deptno group
by b.deptno,dname;

5.Count the total sal deptno wise where more than 2 employees exist.
SELECT deptno, sum(sal) As totalsal
FROM emp
GROUP BY deptno
HAVING COUNT(empno) > 2

6.I have data in a table as seen below:


MONTH VALUE
1 100
2 200
3 300
4 400
5 500
6 600
I want to write a SQL query so that result is given as below:
MONTH_JAN MONTH_FEB MONTH_MAR MONTH_APR MONTH_MAY MONTH_JUN
100 200 300 400 500 600
SELECT SUM(CASE WHEN t.month = 1 THEN t.value ELSE 0 END) AS JAN,
SUM(CASE WHEN t.month = 2 THEN t.value ELSE 0 END) AS FEB,
SUM(CASE WHEN t.month = 3 THEN t.value ELSE 0 END) AS MAR,
SUM(CASE WHEN t.month = 4 THEN t.value ELSE 0 END) AS APR,
SUM(CASE WHEN t.month = 5 THEN t.value ELSE 0 END) AS MAY,
SUM(CASE WHEN t.month = 6 THEN t.value ELSE 0 END) AS JUN
FROM YOUR_TABLE t
7. Determine the name sex and age of the oldest student.
SELECT Name, Gender, (CURRENT_DATE-Dtnaiss)/365 AS Age
FROM Student
WHERE (CURRENT_DATE-Dtnaiss) /365 =
( SELECT MAX(( CURRENT_DATE-Dtnaiss) /365) FROM Student);

8. Display the marks of the student number 1 which are equal to the
marks of the student number 2.
SELECT Note
FROM NOTES
WHERE Num=1 AND Note IN
(SELECT Note FROM NOTES WHERE Num=2);

9. Finding the names of everybody who works in the same department as


a person called James
SELECT name FROM emp WHERE dept_no =
(SELECT dept_no FROM emp WHERE name = 'James')
or as a join statement, like this:-
SELECT e1.name FROM emp e1,emp e2
WHERE e1.dept_no = e2.dept_no AND e2name = 'James'

10. The SQL statement to find the departments that have employees with
a salary higher than the average employee salary
SELECT name FROM dept
WHERE id IN
(
SELECT dept_id FROM emp
WHERE sal >
(SELECT avg(sal)FROM emp)
)
11. Write the SQL to use a sub query which will not return any rows -
when just the table structure is required and not any of the data.
CREATE TABLE new_table AS
SELECT * from table_orig WHERE 1=0;
The sub query returns no data but does return the column names and data types to
the 'create table' statement.

12. HOW DO YOU FIND THE SECOND HIGHEST SALARY?


SELECT MAX(SALARY) FROM EMPLOYEE WHERE SALARY NOT IN (SELECT MAX(SALARY)
FROM EMPLOYEE)

13. Finding duplicates in a table


SELECT name, COUNT (name) AS NumOccurrences FROM users

GROUP BY email HAVING (COUNT (name) > 1)


14. What is difference between Co-related sub query and nested sub query?
Correlated subquery runs once for each row selected by the outer query. It contains a reference to a value from the
row selected by the outer query.
Nested subquery runs only once for the entire nesting (outer) query. It does not contain any reference to the outer
query row.
For example,
Correlated Subquery:
select e1.empname, e1.basicsal, e1.deptno from emp e1 where e1.basicsal = (select max(basicsal) from emp e2
where e2.deptno = e1.deptno)
Nested Subquery:
select empname, basicsal, deptno from emp where (deptno, basicsal) in (select deptno, max(basicsal) from emp
group by deptno)

15. WHAT OPERATOR PERFORMS PATTERN MATCHING?


Pattern matching operator is LIKE and it has to used with two attributes

1. % means matches zero or more characters and


2. _ ( underscore ) means matching exactly one character
16. What is "normalization"? "Denormalization"? Why do you sometimes want to
denormalize?
Normalizing data means eliminating redundant information from a table and organizing the data so that future
changes to the table are easier.

Denormalization means allowing redundancy in a table. The main benefit of denormalization is improved
performance with simplified data retrieval and manipulation. This is done by reduction in the number of joins needed
for data processing.
17. SQL CREATE VIEW Syntax
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
11. In a table how can one find all of the employees whose first name
does not start with 'M' or 'A'.
SELECT EmployeeID, FirstName, LastName, HireDate, City FROM Employees
WHERE (FirstName NOT LIKE 'M%') AND (FirstName NOT LIKE 'A%')

12.Find the wine(s) sold for the highest


Sells(bar, wine, price)

SELECT wine
FROM Sells
WHERE price >= ALL(
SELECT price
FROM Sells
);
wines(name, manf)

13.Find the wines that are the unique wine by their manufacturer
SELECT nameFROM wines w1

WHERE NOT EXISTS(


SELECT *
FROM wines
WHERE manf = w1.manf AND
name <> w1.name
);

14.Find the name and manufacturer of wines that James like


wines (name, manf) Likes(drinker, wine)
SELECT *
FROM wines
WHERE name IN
(SELECT wine
FROM Likes
WHERE drinker = ‘James’
);

15.Find bars that serve Mike at the same price James


charges for Bille.
Sells(bar, wine, price)

SELECT bar
FROM Sells
WHERE wine = 'Mike' AND
price =
(SELECT price
FROM Sells
WHERE bar = 'James''s Bar' AND
wine = 'Bille'
);

16.Find pairs of wines by the same manufacturer.


Wines(name, manf)
SELECT w1.name, w2.name
FROM Wines w1, Wines w2
WHERE w1.manf = w2.manf AND
w1.name < w2.name;

17.Find the wines that the frequenters of James’s Bar


like.
Likes(drinker, wine)
Frequents(drinker, bar)
SELECT wine
FROM Frequents, Likes
WHERE bar = 'James''s Bar' AND
Frequents.drinker = Likes.drinker;

18.Find drinkers whose phone has exchange 555.


Drinkers(name, addr, phone)
SELECT name
FROM Drinkers
WHERE phone LIKE '%555- ';
19.Find the price James's Bar charges for Bille.
Sells(bar, wine, price)
SELECT price
FROM Sells
WHERE bar = 'James''s Bar' AND wine = 'Bille';

20. How to find out the 10th highest salary in SQL query?
Table - Tbl_Test_Salary
Column - int_salary

select max(int_salary)
from Tbl_Test_Salary
where int_salary in(select top 10 int_Salary from
Tbl_Test_Salary order by int_salary)

9. Common SQL Syntax used in database interaction

9a. Select Statement


SELECT "column_name" FROM "table_name"

9b. Distinct
SELECT DISTINCT "column_name" FROM "table_name"

9c. Where
SELECT "column_name" FROM "table_name" WHERE "condition"

9d. And/Or
SELECT "column_name" FROM "table_name" WHERE "simple condition" {[AND|OR] "simple condition"}+

9e. In
SELECT "column_name" FROM "table_name" WHERE "column_name" IN ('value1', 'value2', ...)

9f. Between
SELECT "column_name" FROM "table_name" WHERE "column_name" BETWEEN 'value1' AND 'value2'

9g. Like
SELECT "column_name" FROM "table_name" WHERE "column_name" LIKE {PATTERN}

9h. Order By
SELECT "column_name" FROM "table_name" [WHERE "condition"] ORDER BY "column_name" [ASC, DESC]

9i. Count
SELECT COUNT("column_name") FROM "table_name"
9j. Group By
SELECT "column_name1", SUM("column_name2") FROM "table_name" GROUP BY "column_name1"

9k. Having
SELECT "column_name1", SUM("column_name2") FROM "table_name" GROUP BY "column_name1" HAVING
(arithematic function condition)

9l. Create Table Statement


CREATE TABLE "table_name" ("column 1" "data_type_for_column_1","column 2" "data_type_for_column_2",…)

9m. Drop Table Statement


DROP TABLE "table_name"

9n. Truncate Table Statement


TRUNCATE TABLE "table_name"

9m. Insert Into Statement


INSERT INTO "table_name" ("column1", "column2", ...) VALUES ("value1", "value2", ...)

9o. Update Statement


UPDATE "table_name" SET "column_1" = [new value] WHERE {condition}

9p. Delete From Statement


DELETE FROM "table_name" WHERE {condition}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy