Skip to content

Commit d5e85d6

Browse files
committedFeb 16, 2022
first commit
0 parents  commit d5e85d6

33 files changed

+12341
-0
lines changed
 

‎1.1 SQL_Interview_question_y.txt

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
ddl-drop,rename,create,alter,truncate
2+
dml-delete ,insert, update
3+
dcl-grant,revoke
4+
dql-select
5+
tcl-commit,rollback,save point
6+
7+
Q. DIFFERENCE between delete ,truncate,drop
8+
Q. difference between where and having
9+
Q. difference between group by and order by
10+
11+
LEVEL 2
12+
Q. difference between CHAR AND varchar
13+
Q. difference between union and join
14+
Q. difference between in & exist
15+
Q. how to create empty table with the same structure as another table
16+
select * INTO students_copy from students where 1=2(false conditon 1=2)
17+
Q what is pattern matching in sql
18+
select * form students where name LIKE '_k%'; --SECOND LETTER IS K,% MEANS ANTYHING AFTER THAT
19+
Q. WHAT IS UNION MINUS INTERSECT
20+
union -combines, intersect- means common between both tables will be printed,minus all records from student table which are not in contacts table will be printed
21+
--select name from students UNION select name from contacts
22+
23+
Q difference between union and union all
24+
union print only once, union all can print duplicates
25+
26+
Q what is a view
27+
virtual table : from customers where country='India';
28+
29+
Q character manipulation function
30+
UPPER,LOWER,INITCAP,LENGTH,CONCAT
31+
32+
level 3
33+
NORMAL FOEMS
34+
35+
1. DIFFERENCE BETWEEN WHERE v/s HAVING
36+
1.WHERE
37+
-filter rows
38+
-works on rows data ,not on aggregated data
39+
select * from employee here score>40;
40+
2.HAVING
41+
-works on aggregated data
42+
AGGREGATE FUNCTIONS
43+
- TO perform calculation on multiple rows of a single column
44+
-it returns a single value
45+
-it is used to summarize data
46+
-count,max,min,avg,summarize
47+
-having and group by used in agggregate function
48+
select max(salary) from employee having emp_id>=15
49+
50+
2. DIFFERENCE BETWEEN UNION v/s UNION ALL
51+
UNION - REMOVES duplicate records
52+
UNION ALLL- does not remove duplicate
53+
--union operator combines rsult set of 2 or more select statement
54+
--each select stmt must have
55+
1.same number coln
56+
2.coln must have similar data type
57+
3.coln must be in same order
58+
59+
3. DIFFERENCE BETWEEN IN v/s EXIST
60+
1.IN -> multiple or
61+
select * from customers where city='mumbai' or city='banglore' or city='chennai'
62+
can be converted into like this querry
63+
select * from customers where city IN (MUMBAI,BANGLORE,CHENNAI);
64+
2.EXIST-> returns true or false
65+
select * from customers where EXISTS (select city from where table2.id=customer.id)
66+
USECASE
67+
--IN-> bIG OUTER QUERRY AND SMALL INNER QUERRY
68+
--EXISTS-> SMALL OUTER QUERRY and big inner querry
69+
--IN-> Compare one value to several value
70+
--EXISTS->tells you wheather a querry returned any result
71+
72+
4. DIFFERENCE BETWEEN ORDER BY v/s GROUP BY
73+
ORDER BY-> SORTING,ASC OR DSC
74+
GROUP BY-> USED WITH AGGREGATE FUNCTIONS
75+
*'GROUP' BY FOLLOWS 'where' clause in 'select' statement
76+
*'WHERE' Cannot be used after group by
77+
*'HAVING' IS USED AFTER group by
78+
79+
4. DIFFERENCE BETWEEN JOIN v/s SUBQUERY
80+
-both are used to combine data from different tables into a single result
81+
---SUBQUERRY
82+
SELECT phone, cust_name from customers where cust_id IN
83+
(select cust_id from orders);
84+
-can select only from first table
85+
-slower
86+
---JOIN
87+
SELECT phone, cust_name,ORDER_ID from customers c JOIN orders o on c.cust_id=o.cust_id;
88+
-can select form either of the table
89+
-faster
90+
91+
4. DIFFERENCE BETWEEN JOIN v/s UNION
92+
---union
93+
-cobines rows
94+
-not neccessary to have same column name
95+
-but no. of column & datatype of column should be same
96+
---JOIN
97+
-MERGES column
98+
-combines rows from 2 or more tables based on a related common column between them
99+
100+
Q. how to delete duplicate rows from a table in SQL.
101+
4 following ways are shown:
102+
1) remove duplicate rows using a temporary table
103+
2) remove duplicate rows using auto ID
104+
3) remove duplicate rows using row_number()
105+
4) remove duplicate rows using CTE
106+
107+
1.USING TEMP TABLE
108+
-SELECT distinct * INTO NEW_TABLE FROM OLD_TABLE
109+
-DELETE * FORM OLD_TABLE or TRUNCATE
110+
-INSERT INTO OLD_TABLE SELECT * FROM NEW_TABLE
111+
-DROP TABLE NEW_TABLE
112+
113+
2. USING AUTO_ID
114+
-ALTER TABLE EMPLOYEE ADD auto_id int IDENTITY(1,1)
115+
-delete 8 from employee where auto_id NOT IN (SELECT min(auto_id)
116+
from employee group by emp_id ,emp_name)
117+
118+
3.using row number
119+
delete from (select 8, row_number() OVER(PARTITION BY ID Order by id) as rn from mytable)
120+
where rn>1
121+
122+
4.USING CTE (COMMON TABLE EXPRESSION)
123+
-used as a temporary result set
124+
-lasts only for duration for the querry
125+
126+
WITH cte as (select row_number() over(partition by id order by id) as rn from mytable)
127+
delete from cte rn>1
128+
129+
5. DIFFERENCE BETWEEN DELETE v/s TRUNCATE
130+
---DELETE
131+
-DML ,DELETE REMOVEs some or all rows
132+
-it does not free space containing the table
133+
-the transaction log will still have the deleted rows
134+
-slower than truncate
135+
---TRUNCATE
136+
-DDL,removes all the rows from the table
137+
-it frees up space containing the table
138+
-no where clause can be used
139+
---DROP
140+
-removes table from the database
141+
-cannot be rolled up
142+
143+
Q8.difference BETWEEN CHAR V/S VARCHAR
144+
VARCHAR->variable length
145+
char->fixed length
146+
---char
147+
-uses stTIC MEMORY ALLOCATION
148+
-FIXED length
149+
-faster
150+
---VARCHAR
151+
-it uses dynamic memory location
152+
-variable length
153+
-better for storage
154+
-slower
155+
-has an overhead of 2 bytes
156+
---char->telephone no,zip code, etc
157+
---varchar->names
158+

‎1.1 SQL_Interview_question_y.txt.bak

+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
ddl-drop,rename,create,alter,truncate
2+
dml-delete ,insert, update
3+
dcl-grant,revoke
4+
dql-select
5+
tcl-commit,rollback,save point
6+
7+
Q. DIFFERENCE between delete ,truncate,drop
8+
Q. difference between where and having
9+
Q. difference between group by and order by
10+
11+
LEVEL 2
12+
Q. difference between CHAR AND varchar
13+
Q. difference between union and join
14+
Q. difference between in & exist
15+
Q. how to create empty table with the same structure as another table
16+
select * INTO students_copy from students where 1=2(false conditon 1=2)
17+
Q what is pattern matching in sql
18+
select * form students where name LIKE '_k%'; --SECOND LETTER IS K,% MEANS ANTYHING AFTER THAT
19+
Q. WHAT IS UNION MINUS INTERSECT
20+
union -combines, intersect- means common between both tables will be printed,minus all records from student table which are not in contacts table will be printed
21+
--select name from students UNION select name from contacts
22+
23+
Q difference between union and union all
24+
union print only once, union all can print duplicates
25+
26+
Q what is a view
27+
virtual table : from customers where country='India';
28+
29+
Q character manipulation function
30+
UPPER,LOWER,INITCAP,LENGTH,CONCAT
31+
32+
level 3
33+
NORMAL FOEMS
34+
35+
1. DIFFERENCE BETWEEN WHERE v/s HAVING
36+
1.WHERE
37+
-filter rows
38+
-works on rows data ,not on aggregated data
39+
select * from employee here score>40;
40+
2.HAVING
41+
-works on aggregated data
42+
AGGREGATE FUNCTIONS
43+
- TO perform calculation on multiple rows of a single column
44+
-it returns a single value
45+
-it is used to summarize data
46+
-count,max,min,avg,summarize
47+
-having and group by used in agggregate function
48+
select max(salary) from employee having emp_id>=15
49+
50+
2. DIFFERENCE BETWEEN UNION v/s UNION ALL
51+
UNION - REMOVES duplicate records
52+
UNION ALLL- does not remove duplicate
53+
--union operator combines rsult set of 2 or more select statement
54+
--each select stmt must have
55+
1.same number coln
56+
2.coln must have similar data type
57+
3.coln must be in same order
58+
59+
3. DIFFERENCE BETWEEN IN v/s EXIST
60+
1.IN -> multiple or
61+
select * from customers where city='mumbai' or city='banglore' or city='chennai'
62+
can be converted into like this querry
63+
select * from customers where city IN (MUMBAI,BANGLORE,CHENNAI);
64+
2.EXIST-> returns true or false
65+
select * from customers where EXISTS (select city from where table2.id=customer.id)
66+
USECASE
67+
--IN-> bIG OUTER QUERRY AND SMALL INNER QUERRY
68+
--EXISTS-> SMALL OUTER QUERRY and big inner querry
69+
--IN-> Compare one value to several value
70+
--EXISTS->tells you wheather a querry returned any result
71+
72+
4. DIFFERENCE BETWEEN ORDER BY v/s GROUP BY
73+
ORDER BY-> SORTING,ASC OR DSC
74+
GROUP BY-> USED WITH AGGREGATE FUNCTIONS
75+
*'GROUP' BY FOLLOWS 'where' clause in 'select' statement
76+
*'WHERE' Cannot be used after group by
77+
*'HAVING' IS USED AFTER group by
78+
79+
4. DIFFERENCE BETWEEN JOIN v/s SUBQUERY
80+
-both are used to combine data from different tables into a single result
81+
---SUBQUERRY
82+
SELECT phone, cust_name from customers where cust_id IN
83+
(select cust_id from orders);
84+
-can select only from first table
85+
-slower
86+
---JOIN
87+
SELECT phone, cust_name,ORDER_ID from customers c JOIN orders o on c.cust_id=o.cust_id;
88+
-can select form either of the table
89+
-faster
90+
91+
4. DIFFERENCE BETWEEN JOIN v/s UNION
92+
---union
93+
-cobines rows
94+
-not neccessary to have same column name
95+
-but no. of column & datatype of column should be same
96+
---JOIN
97+
-MERGES column
98+
-combines rows from 2 or more tables based on a related common column between them
99+
100+
Q. how to delete duplicate rows from a table in SQL.
101+
4 following ways are shown:
102+
1) remove duplicate rows using a temporary table
103+
2) remove duplicate rows using auto ID
104+
3) remove duplicate rows using row_number()
105+
4) remove duplicate rows using CTE
106+
107+
1.USING TEMP TABLE
108+
-SELECT distinct * INTO NEW_TABLE FROM OLD_TABLE
109+
-DELETE * FORM OLD_TABLE or TRUNCATE
110+
-INSERT INTO OLD_TABLE SELECT * FROM NEW_TABLE
111+
-DROP TABLE NEW_TABLE
112+
113+
2. USING AUTO_ID
114+
-ALTER TABLE EMPLOYEE ADD auto_id int IDENTITY(1,1)
115+
-delete 8 from employee where auto_id NOT IN (SELECT min(auto_id)
116+
from employee group by emp_id ,emp_name)
117+
118+
3.using row number
119+
delete from (select 8, row_number() OVER(PARTITION BY ID Order by id) as rn from mytable)
120+
where rn>1
121+
122+
4.USING CTE (COMMON TABLE EXPRESSION)
123+
-used as a temporary result set
124+
-lasts only for duration for the querry
125+
126+
WITH cte as (select row_number() over(partition by id order by id) as rn from mytable)
127+
delete from cte rn>1
128+
129+
5. DIFFERENCE BETWEEN DELETE v/s TRUNCATE
130+
---DELETE
131+
-DML ,DELETE REMOVEs some or all rows
132+
-it does not free space containing the table
133+
-the transaction log will still have the deleted rows
134+
-slower than truncate
135+
---TRUNCATE
136+
-DDL,removes all the rows from the table
137+
-it frees up space containing the table
138+
-no where clause can be used
139+
---DROP
140+
-removes table from the database
141+
-cannot be rolled up
142+
#Q1.increase income of all employees by 5% in a table
143+
update employees SET income=income+(income * 5.0/100.0);
144+
145+
146+
#find names of employees starting with 'A'
147+
select first_name fro employees where first_name LIKE 'A%';
148+
149+
#Q3. find no. of employees working in department 'ABC'
150+
SELECT COUNT FROM EMPLOYEES WHERE department_name='ABC';
151+
152+
#Q4. PRINT DETAILS OF EMPLOYEES WHOSE FIRST_NAME END WITH 'A' AND CONTAINS 6 alphabets
153+
select * from employees where first_name LIKE '______A'
154+
155+
#Q5.print details of employees whose salary lies between 10000 and 50000
156+
SELECT * FROM EMPLOYEES WHERE SALARY BETWEEN 10000 AND 50000
157+
158+
Q8.difference BETWEEN CHAR V/S VARCHAR
159+
VARCHAR->variable length
160+
char->fixed length
161+
---char
162+
-uses stTIC MEMORY ALLOCATION
163+
-FIXED length
164+
-faster
165+
---VARCHAR
166+
-it uses dynamic memory location
167+
-variable length
168+
-better for storage
169+
-slower
170+
-has an overhead of 2 bytes
171+
---char->telephone no,zip code, etc
172+
---varchar->names
173+
Q. fetch to n recordsselec top n* from employees order by salary desc
174+
175+
Q. retrive empfname,and emplname in a single column as 'fullname', first name ,last name
176+
seprated with space
177+
--select CONCAT(empfname,' ',emplname) as 'Fullname' from employees;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#RANK, DENSE_RANK, ROW_NUMBER
2+
#jiski slary zyaya usko top rank
3+
#RANK===GIVE SAME RANK TO SAME SALARY
4+
select emp_id, emp_name ,department_id, salary,
5+
rank() OVER(order by salary desc) as rnk
6+
from emp;
7+
8+
##DENSE RANK== two peple with same slary get same rank,dense rank dont skip number like rank
9+
10+
select emp_id, emp_name ,department_id, salary
11+
,RANK() OVER(order by salary desc) as rnk
12+
,DENSE_RANK() OVER(order by salary desc) as dens_rnk
13+
from emp;
14+
15+
## give different trank to same nos
16+
select emp_id, emp_name ,department_id, salary
17+
,RANK() OVER(order by salary desc) as rnk
18+
,DENSE_RANK() OVER(order by salary desc) as dens_rnk
19+
,row_number() OVER(order by salary desc) as rows_number
20+
from emp;
21+
##IF WANT DEPARTMENT WISE RANK,ADD A CLAUSE PARTITION BY
22+
23+
select emp_id, emp_name ,department_id, salary
24+
,RANK() OVER(partition by department_id ORDER by salary desc) as rnk
25+
,DENSE_RANK() OVER(partition by department_id order by salary desc) as dens_rnk
26+
,row_number() OVER(partition by department_id order by salary desc) as rows_number
27+
from emp;
28+
29+
##IF WANT DEPARTMENT WISE HIGHEST SALARY,ADD PARTITION BY DEPARTMENT
30+
select * from (
31+
select emp_id, emp_name ,department_id, salary
32+
,RANK() OVER(partition by department_id ORDER by salary desc) as rnk
33+
from emp ) a
34+
WHERE rnk=1;
35+
36+
37+
#create table emp
38+
#(
39+
#emp_id int,
40+
#emp_name Varchar(20),
41+
#department_id Varchar(20),
42+
#salary int
43+
#);
44+
#INSERT INTO emp values(1,'Ankit',100,10000);
45+
#INSERT INTO emp values(2,'Mohit',100,15000);
46+
#INSERT INTO emp values(3,'Vikas',100,10000);
47+
#INSERT INTO emp values(4,'Rohit',100,5000);
48+
#INSERT INTO emp values(5,'Mudit',200,12000);
49+
#INSERT INTO emp values(6,'Agam',200,12000);
50+
#INSERT INTO emp values(7,'Sanjay',200,9000);
51+
#INSERT INTO emp values(8,'Ashish',200,5000);

0 commit comments

Comments
 (0)
Please sign in to comment.