100% found this document useful (1 vote)
169 views8 pages

SQL Interview

This document contains 29 SQL queries examples to solve common interview questions. The queries cover topics such as finding the nth highest salary, top n records, department counts, string manipulation, joins, filtering on conditions, aggregations, and more. The document serves as a reference for preparing for SQL interview questions.

Uploaded by

abhishek pathak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
169 views8 pages

SQL Interview

This document contains 29 SQL queries examples to solve common interview questions. The queries cover topics such as finding the nth highest salary, top n records, department counts, string manipulation, joins, filtering on conditions, aggregations, and more. The document serves as a reference for preparing for SQL interview questions.

Uploaded by

abhishek pathak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

10/10/2022, 08:19 SQL-Interview-Prep-Question/queries.

sql at master · Aafreen29/SQL-Interview-Prep-Question · GitHub

Aafreen29/SQL-Interview-Prep-Question Public

Code Issues 1 Pull requests 3 Actions Projects Security Insights

master
SQL-Interview-Prep-Question / queries.sql
Aafreen29 Update queries.sql
1 contributor

366 lines (286 sloc)



14.7 KB

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

31 select * from employee;


32 select * from bonus;
https://github.com/Aafreen29/SQL-Interview-Prep-Question/blob/master/queries.sql 1/8
10/10/2022, 08:19 SQL-Interview-Prep-Question/queries.sql at master · Aafreen29/SQL-Interview-Prep-Question · GitHub
33
34 select * from employee where employee_id in (select employee_ref_id from bonus where em
35
36 -- 8> Write a SQL query to find only odd rows from employee table
37 select * from employee where MOD(employee_id,2)<>0;
38
39 -- 9> Write a SQL query to fetch first_name from employee table in upper case
40 select upper(first_name) as First_Name from employee;
41
42 -- 10> Write a SQL query to get combine name (first name and last name) of employees fr
43 select concat(first_name, ' ' ,last_name) as Name from employee;
44
45 -- 11> Write a SQL query to print details of employee of employee 'Jennifer' and 'James
46 select * from employee where first_name in ('Jennifer', 'James');
47
48 -- 12> Write a SQL query to fetch records of employee whose salary lies between
49 select first_name, last_name, salary from employee where salary between 100000 and 5000
50
51 -- 13> Write a SQL query to get records of employe who have joined in Jan 2017
52 select * from employee;
53
54 select first_name, last_name, joining_date from employee where year(joining_date)=2017
55
56 -- 14> Write a SQL query to get the list of employees with the same salary
57 select e1.first_name, e2.last_name from employee e1, employee e2 where e1.salary = e2.s
58
59 -- 15> Write a SQL query to show all departments along with the number of people workin
60 select * from employee;
61
62 select department, count(*) as 'Number of employees' from employee
63 group by department
64 order by count(department);
65
66 -- 16> Write a SQL query to show the last record from a table.
67 select * from employee where employee_id = (select max(employee_id) from employee);
68
69 -- 17> Write a SQL query to show the first record from a table.
70 select * from employee where employee_id = (select min(employee_id) from employee);
71
72 -- 18> Write a SQL query to get last five records from a employee table.
73 (select * from employee order by employee_id desc limit 5) order by employee_id;
74
75 -- 19> Write a SQL query to find employees having the highest salary in each department
76 select first_name, last_name, department, max(salary) as 'Max Salary'from employee grou
77
78 -- 20> Write a SQL query to fetch three max salaries from employee table.
79 select distinct salary from employee order by salary desc limit 3 ;
80 -- OR-----
81 select distinct Salary from employee e1 WHERE 3 >= (SELECT count(distinct Salary) from
82

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

135 where a.date_action = '2018-05-24' and b.action = "accepted";


136
https://github.com/Aafreen29/SQL-Interview-Prep-Question/blob/master/queries.sql 3/8
10/10/2022, 08:19 SQL-Interview-Prep-Question/queries.sql at master · Aafreen29/SQL-Interview-Prep-Question · GitHub
137 -- 29> How many total users follow sport accounts from tables all_users, sport_accounts
138 select * from all_users;
139 select * from sport_accounts;
140 select * from follow_relation;
141
142 select count(distinct c.follower_id) as count_all_sports_followers
143 from sport_accounts a
144 join all_users b
145 on a.sport_player_id = b.user_id
146 join follow_relation c
147 on b.user_id = c.target_id;
148
149 -- 30> How many active users follow each type of sport?
150
151 select b.sport_category, count(a.user_id)
152 from all_users a
153 join sport_accounts b
154 on a.user_id = b.sport_player_id
155 join follow_relation c
156 on a.user_id = c.follower_id
157 where a.active_last_month =1
158 group by b.sport_category;
159
160 -- 31> What percent of active accounts are fraud from ad_accounts table?
161 select * from ad_accounts;
162
163 select count(distinct a.account_id)/(select count(account_id) from ad_accounts where ac
164 from ad_accounts a
165 join ad_accounts b
166 on a.account_id = b.account_id
167 where a.account_status = 'fraud' and b.account_status='active';
168
169 -- 32> How many accounts became fraud today for the first time from ad_accounts table?
170
171 select count(account_id) 'First time fraud accounts' from (
172 select distinct a.account_id, count(a.account_status)
173 from ad_accounts a
174 join ad_accounts b
175 on a.account_id = b.account_id
176 where b.date = curdate() and a.account_status = 'fraud'
177 group by account_id
178 having count(a.account_status) = 1) ad_accnt;
179
180 -- 33> Write a SQL query to determine avg time spent per user per day from user_details
181 select * from event_session_details;
182 select * from user_details;
183
184 select date, user_id, sum(timespend_sec)/count(*) as 'avg time spent per user per day'
185 from event_session_details
186 group by 1,2

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 (

239 select requestor_id as id from new_request_accepted


240 union all
https://github.com/Aafreen29/SQL-Interview-Prep-Question/blob/master/queries.sql 5/8
10/10/2022, 08:19 SQL-Interview-Prep-Question/queries.sql at master · Aafreen29/SQL-Interview-Prep-Question · GitHub
241 select acceptor_id as id from new_request_accepted) as a
242 group by 1
243 order by count desc
244 limit 1) as table1;
245
246 -- 39> from the table count_request, find total count of requests sent and total count
247 -- per country
248 select * from count_request;
249
250 select country_code, Total_request_sent, Total_percent_of_request_sent_failed,
251 cast((Total_request_sent*Total_percent_of_request_sent_failed)/100 as decimal) as Total
252 from
253 (
254 select country_code, sum(count_of_requests_sent) as Total_request_sent,
255 cast(replace(ifnull(sum(percent_of_request_sent_failed),0), '%','') as decimal(2,1)) as
256 from count_request
257 group by country_code
258 ) as Table1;
259
260 -- 40> create a histogram of duration on x axis, no of users on y axis which is populat
261 -- from event_session_details
262 select * from event_session_details;
263
264 select floor(timespend_sec/500)*500 as bucket,
265 count(distinct user_id) as count_of_users
266 from event_session_details
267 group by 1;
268
269 -- 41> Write SQL query to calculate percentage of confirmed messages from two tables :
270 -- confirmation_no (phone numbers that facebook sends the confirmation messages to) and
271 -- confirmed_no (phone numbers that confirmed the verification)
272
273 select round((count(confirmed_no.phone_number)/count(confirmation_no.phone_number))*100
274 from confirmation_no
275 left join confirmed_no
276 on confirmed_no.phone_number= confirmation_no.phone_number;
277
278 -- 42> Write SQL query to find number of users who had 4 or more than 4 interactions on
279 -- from user_interaction table (user_1, user_2, date).
280 -- assume there is only one unique interaction between a pair of users per day
281
282 select * from user_interaction;
283
284 select table1.user_id, sum(number_of_interactions) as Number_of_interactions
285 from
286 (
287 select user_1 as user_id, count(user_1) as number_of_interactions from user_interaction
288 group by user_1
289 union all
290 select user_2 as user_id, count(user_2) as number_of_interactions from user_interaction

291 group by user_2) table1


292 group by table1.user_id
https://github.com/Aafreen29/SQL-Interview-Prep-Question/blob/master/queries.sql 6/8
10/10/2022, 08:19 SQL-Interview-Prep-Question/queries.sql at master · Aafreen29/SQL-Interview-Prep-Question · GitHub
293 having sum(number_of_interactions) >= 4;
294
295 -- 43> write a sql query to find the names of all salesperson that have order with sams
296 -- the table: salesperson, customer, orders
297
298 select s.name
299 from salesperson s
300 join orders o on s.id = o.salesperson_id
301 join customer c on o.cust_id = c.id
302 where c.name = 'Samsonic';
303
304 -- 44> write a sql query to find the names of all salesperson that do not have any orde
305
306 select s.Name
307 from Salesperson s
308 where s.ID NOT IN(
309 select o.salesperson_id from Orders o, Customer c
310 where o.cust_id = c.ID
311 and c.Name = 'Samsonic');
312
313 -- 45> Wrie a sql query to find the names of salespeople that have 2 or more orders.
314 select s.name as 'salesperson', count(o.number) as 'number of orders'
315 from salesperson s
316 join orders o on s.id = o.salesperson_id
317 group by s.name
318 having count(o.number)>=2;
319
320 -- 46> Given two tables: User(user_id, name, phone_num) and UserHistory(user_id, date,
321 -- write a sql query that returns the name, phone number and most recent date for any u
322 -- over the last 30 days
323 -- (you can tell a user has logged in if action field in UserHistory is set to 'logged_
324
325 select user.name, user.phone_num, max(userhistory.date)
326 from user,userhistory
327 where user.user_id = userhistory.user_id
328 and userhistory.action = 'logged_on'
329 and userhistory.date >= date_sub(curdate(), interval 30 day)
330 group by user.name;
331
332 -- 47> Given two tables: User(user_id, name, phone_num) and UserHistory(user_id, date,
333 -- Write a SQL query to determine which user_ids in the User table are not contained in
334 -- (assume the UserHistory table has a subset of the user_ids in User table). Do not us
335 -- Note: the UserHistory table can have multiple entries for each user_id.
336 select user.user_id
337 from user
338 left join userhistory
339 on user.user_id = userhistory.user_id
340 where userhistory.user_id is null;
341
342 -- 48> from a given table compare(numbers int(4)), write a sql query that will return t

343 -- from the numbers without using


344 -- sql aggregate like max or min
https://github.com/Aafreen29/SQL-Interview-Prep-Question/blob/master/queries.sql 7/8
10/10/2022, 08:19 SQL-Interview-Prep-Question/queries.sql at master · Aafreen29/SQL-Interview-Prep-Question · GitHub
345
346 select numbers
347 from compare
348 order by numbers desc
349 limit 1;
350
351 -- 49> Write a SQL query to find out how many users inserted more than 1000 but less th
352 -- There is a startup company that makes an online presentation software and they have
353 -- an image into a presentation. one user can insert multiple images
354
355 select count(*) from
356 (select user_id, count(event_date_time) as image_per_user
357 from event_log
358 group by user_id) as image_per_user
359 where image_per_user <2000 and image_per_user>1000;
360
361 -- 50> select the most recent login time by values from the login_info table
362
363 select * from login_info
364 where login_time in (select max(login_time) from login_info
365 group by user_id)
366 order by login_time desc limit 1;

https://github.com/Aafreen29/SQL-Interview-Prep-Question/blob/master/queries.sql 8/8

You might also like