SQL Interview
SQL Interview
Aafreen29/SQL-Interview-Prep-Question Public
master
SQL-Interview-Prep-Question / queries.sql
Aafreen29 Update queries.sql
1 contributor
1 -- 1> Write a SQL query to find the nth highest salary from employee table.
2 -- Example: finding 3rd highest salary from employee table
3 select * from employee order by salary desc;
4 --- Limit N-1,1
5 select distinct salary from employee order by salary desc limit 2, 1;
6
7 -- 2> Write a SQL query to find top n records?
8 -- Example: finding top 5 records from employee table
9 select * from employee order by salary desc limit 5;
10
11 -- 3> Write a SQL query to find the count of employees working in department 'Admin'
12 select count(*) from employee where department = 'Admin';
13
14 -- 4> Write a SQL query to fetch department wise count employees sorted by department c
15 select * from employee;
16
17 select department, count(*) as employeecount
18 from employee
19 group by department
20 order by employeecount desc;
21
22 -- 5> Write a query to fetch only the first name(string before space) from the FullNam
23 select distinct(substring_index(full_names, ' ', 1)) first_name from user_name;
24
25 -- 6> Write a SQL query to find all the employees from employee table who are also mana
26 select e1.first_name, e2.last_name from employee e1
27 join employee e2
28 on e1.employee_id = e2.manager_id;
29
30 -- 7> Write a SQL query to find all employees who have bonus record in bonus table
83 -- 21> Write a SQL query to fetch departments along with the total salaries paid for ea
84 select department, sum(salary) as 'Total Salary' from employee group by department orde
https://github.com/Aafreen29/SQL-Interview-Prep-Question/blob/master/queries.sql 2/8
10/10/2022, 08:19 SQL-Interview-Prep-Question/queries.sql at master · Aafreen29/SQL-Interview-Prep-Question · GitHub
85
86 -- 22> Write a SQL query to find employee with highest salary in an organization from e
87 select first_name, last_name from employee where salary = (select max(salary) from empl
88
89 -- 23> Write an SQL query that makes recommendations using the pages that your fri
90 -- Assume you have two tables: a two-column table of users and their friends, and a two
91 -- users and the pages they liked. It should not recommend pages you already like.
92
93 -- 24> write a SQL query to find employee (first name, last name, department and bonus)
94 select first_name, last_name, department, max(bonus_amount) from employee e
95 join bonus b
96 on e.employee_id = b.employee_ref_id
97 group by department
98 order by max(bonus_amount) desc limit 1;
99
100 -- 25> write a SQL query to find employees with same salary
101 select e1.first_name, e1.last_name, e1.salary from employee e1, employee e2
102 where e1.salary = e2.salary
103 and e1.employee_id != e2.employee_id;
104
105 -- 26> Write SQL to find out what percent of students attend school on their birthday f
106 select * from all_students;
107 select * from attendance_events;
108
109 select (count(attendance_events.student_id) * 100 / (select count(student_id) from atte
110 from attendance_events
111 join all_students
112 on all_students.student_id = attendance_events.student_id
113 where month(attendance_events.date_event) = month(all_students.date_of_birth)
114 and day(attendance_events.date_event) = day(all_students.date_of_birth);
115
116 -- 27> Given timestamps of logins, figure out how many people on Facebook were active a
117 -- of a week on a mobile phone from login info table?
118
119 select * from login_info;
120
121 select a.login_time, count(distinct a.user_id) from
122 login_info a
123 Left join login_info b
124 on a.user_id = b.user_id
125 where a.login_time = b.login_time - interval 1 day
126 group by 1;
127
128 -- 28> Write a SQL query to find out the overall friend acceptance rate for a given dat
129 select * from user_action;
130
131 select count(a.user_id_who_sent)*100 / (select count(user_id_who_sent) from user_action
132 from user_action a
133 join user_action b
134 on a.user_id_who_sent = b.user_id_who_sent and a.user_id_to_whom = b.user_id_to_whom
187 order by 1;
188
https://github.com/Aafreen29/SQL-Interview-Prep-Question/blob/master/queries.sql 4/8
10/10/2022, 08:19 SQL-Interview-Prep-Question/queries.sql at master · Aafreen29/SQL-Interview-Prep-Question · GitHub
189 -- or --
190
191 select date, user_id, avg(timespend_sec)
192 from event_session_details
193 group by 1,2
194 order by 1;
195
196 -- 34> write a SQL query to find top 10 users that sent the most messages from messages
197 select * from messages_detail;
198
199 select user_id, messages_sent
200 from messages_detail
201 order by messages_sent desc
202 limit 10;
203
204 -- 35> Write a SQL query to find disctinct first name from full user name from usere_na
205 select * from user_name;
206
207 select distinct(substring_index(full_names, ' ', 1)) first_name from user_name;
208
209 -- 36> You have a table with userID, appID, type and timestamp. type is either 'click'
210 -- Calculate the click through rate from dialoglog table. Now do it in for each app.
211 -- click through rate is defined as (number of clicks)/(number of impressions)
212 select * from dialoglog;
213
214 select app_id
215 , ifnull(sum(case when type = 'click' then 1 else 0 end)*1.0
216 / sum(case when type = 'impression' then 1 else 0 end), 0 )AS 'CTR(click throug
217 from dialoglog
218 group by app_id;
219
220 -- 37> Given two tables Friend_request (requestor_id, sent_to_id, time),
221 -- Request_accepted (acceptor_id, requestor_id, time). Find the overall acceptance rate
222 -- Overall acceptate rate of requests = total number of acceptance / total number of re
223 select * from friend_request;
224 select * from request_accepted;
225
226 select ifnull(round(
227 (select count(*) from (select distinct acceptor_id, requestor_id from request_accepted)
228 /
229 (select count(*) from (select distinct requestor_id, sent_to_id from friend_request ) a
230 ) as basic;
231
232 -- 38> from a table of new_request_accepted, find a user with the most friends.
233 select * from new_request_accepted;
234
235 select id from
236 (
237 select id, count(*) as count
238 from (
https://github.com/Aafreen29/SQL-Interview-Prep-Question/blob/master/queries.sql 8/8