├── README.md ├── Schemas.sql └── queries.sql /README.md: -------------------------------------------------------------------------------- 1 | # SQL-Interview-Prep-Question 2 | This repository is for everyone who is preparing for Data Scientist/Data Analyst position interviews. 3 | 4 | This effort of mine is a compilation of all the SQL interview questions which I came across through many interviews and also some are frequently asked questions from various sources. Questions shared are from Facebook, Visa, Wayfair, and couple of startups I have interviewed with. These are MySQL queries and I have tested all the queries for its correctness before pushing it in here. 5 | 6 | Please let me know if you have any other way of writing any piece of queries. Your inputs would be greatly appreciated. 7 | -------------------------------------------------------------------------------- /Schemas.sql: -------------------------------------------------------------------------------- 1 | 2 | CREATE TABLE Employee ( 3 | EMPLOYEE_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT, 4 | FIRST_NAME CHAR(25), 5 | LAST_NAME CHAR(25), 6 | SALARY INT(15), 7 | JOINING_DATE DATETIME, 8 | DEPARTMENT CHAR(25), 9 | MANAGER_ID INT 10 | ); 11 | 12 | INSERT INTO Employee 13 | (EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY, JOINING_DATE, DEPARTMENT,MANAGER_ID) VALUES 14 | (001, 'James', 'Smith', 100000, '17-02-20 09.00.00', 'HR', 002), 15 | (002, 'Jessica', 'Kohl', 80000, '17-06-11 09.00.00', 'Admin', 005), 16 | (003, 'Alex', 'Garner', 300000, '17-02-20 09.00.00', 'HR', 011), 17 | (004, 'Pratik', 'Pandey', 500000, '17-02-20 09.00.00', 'Admin', 020), 18 | (005, 'Christine', 'Robinson', 500000, '17-06-11 09.00.00', 'Admin', 007), 19 | (006, 'Deepak', 'Gupta', 200000, '17-06-11 09.00.00', 'Account', 015), 20 | (007, 'Jennifer', 'Paul', 75000, '17-01-20 09.00.00', 'Account', 012), 21 | (008, 'Deepika', 'Sharma', 90000, '17-04-11 09.00.00', 'Admin', 017); 22 | 23 | CREATE TABLE Bonus ( 24 | EMPLOYEE_REF_ID INT, 25 | BONUS_AMOUNT INT(10), 26 | BONUS_DATE DATETIME, 27 | FOREIGN KEY (EMPLOYEE_REF_ID) 28 | REFERENCES Employee(EMPLOYEE_ID) 29 | ON DELETE CASCADE 30 | ); 31 | 32 | INSERT INTO Bonus 33 | (EMPLOYEE_REF_ID, BONUS_AMOUNT, BONUS_DATE) VALUES 34 | (001, 5000, '18-02-20'), 35 | (002, 3000, '18-06-11'), 36 | (003, 4000, '18-02-20'), 37 | (001, 4500, '18-02-20'), 38 | (002, 3500, '18-06-11'); 39 | 40 | CREATE TABLE Title ( 41 | EMPLOYEE_REF_ID INT, 42 | EMPLOYEE_TITLE CHAR(25), 43 | AFFECTED_FROM DATETIME, 44 | FOREIGN KEY (EMPLOYEE_REF_ID) 45 | REFERENCES Employee(EMPLOYEE_ID) 46 | ON DELETE CASCADE 47 | ); 48 | 49 | INSERT INTO Title 50 | (EMPLOYEE_REF_ID, EMPLOYEE_TITLE, AFFECTED_FROM) VALUES 51 | (001, 'Manager', '2018-02-20 00:00:00'), 52 | (002, 'Executive', '2018-06-11 00:00:00'), 53 | (008, 'Executive', '2018-06-11 00:00:00'), 54 | (005, 'Manager', '2018-06-11 00:00:00'), 55 | (004, 'Asst. Manager', '2018-06-11 00:00:00'), 56 | (007, 'Executive', '2018-06-11 00:00:00'), 57 | (006, 'Lead', '2018-06-11 00:00:00'), 58 | (003, 'Lead', '2018-06-11 00:00:00'); 59 | 60 | 61 | CREATE TABLE all_students ( 62 | student_id INT NOT NULL PRIMARY KEY, 63 | school_id INT, 64 | grade_level INT, 65 | date_of_birth DATETIME, 66 | hometown CHAR(25) 67 | ); 68 | CREATE TABLE attendance_events ( 69 | date_event DATETIME, 70 | student_id INT, 71 | attendance CHAR(20), 72 | FOREIGN KEY (student_id) 73 | REFERENCES all_students(student_id) 74 | ON DELETE CASCADE 75 | ); 76 | 77 | INSERT INTO attendance_events 78 | (date_event, student_id, attendance) VALUES 79 | ('2018-01-10', 110111, 'present'), 80 | ('2018-01-10', 110121, 'present' ), 81 | ('2018-01-12', 110115, 'absent'), 82 | ('2018-01-13', 110119, 'absent'), 83 | ('2018-01-13', 110121, 'present'), 84 | ('2018-01-14', 110125, 'present'), 85 | ('2018-02-05', 110111, 'absent'), 86 | ('2018-02-17', 110115, 'present'), 87 | ('2018-02-22', 110129, 'absent'); 88 | 89 | INSERT INTO all_students 90 | (student_id, school_id, grade_level, date_of_birth, hometown) VALUES 91 | (110111, 205, 1, '2013-01-10', 'Pleasanton'), 92 | (110115, 205, 1, '2013-03-15', 'Dublin'), 93 | (110119, 205, 2, '2012-02-13', 'San Ramon'), 94 | (110121, 205, 3, '2011-01-13', 'Dublin'), 95 | (110125, 205, 2, '2012-04-25','Dublin'), 96 | (110129, 205, 3, '2011-02-22', 'San Ramon'); 97 | 98 | CREATE TABLE login_info ( 99 | user_id INT, 100 | login_time DATETIME 101 | ); 102 | 103 | INSERT INTO login_info 104 | (user_id, login_time) VALUES 105 | (1, '2017-08-10 14:32:25'), 106 | (2, '2017-08-11 14:32:25'), 107 | (3, '2017-08-11 14:32:25'), 108 | (2, '2017-08-13 14:32:25'), 109 | (3, '2017-08-14 14:32:25'), 110 | (4, '2017-08-15 14:32:25'), 111 | (5, '2017-08-12 14:32:25'), 112 | (2, '2017-08-18 14:32:25'), 113 | (1, '2017-08-11 14:32:25'), 114 | (1, '2017-08-12 14:32:25'), 115 | (1, '2017-08-13 14:32:25'), 116 | (1, '2017-08-14 14:32:25'), 117 | (1, '2017-08-15 14:32:25'), 118 | (1, '2017-08-16 14:32:25'), 119 | (1, '2017-08-17 14:32:25'), 120 | (3, '2017-08-20 14:32:25'), 121 | (5, '2017-08-16 14:32:25'), 122 | (2, '2017-08-21 14:32:25'), 123 | (3, '2017-08-22 14:32:25'); 124 | 125 | CREATE TABLE USER_ACTION ( 126 | user_id_who_sent INT, 127 | user_id_to_whom INT, 128 | date_action DATETIME, 129 | action CHAR(25) 130 | ); 131 | 132 | INSERT INTO USER_ACTION 133 | (user_id_who_sent, user_id_to_whom, date_action, action) VALUES 134 | (20251, 28272, '2018-05-24','accepted'), 135 | (19209, 64638,'2018-06-13' , 'sent'), 136 | (43744, 16373, '2018-04-15' ,'accepted'), 137 | (20251, 18171, '2018-05-19' , 'sent'), 138 | (54875, 36363, '2018-01-11' ,'rejected'), 139 | (38292, 16373, '2018-05-24','accepted'), 140 | (19209, 26743, '2018-06-12' ,'accepted'), 141 | (27623, 28272, '2018-05-24','accepted'), 142 | (20251, 37378, '2018-03-17','rejected'), 143 | (43744, 18171, '2018-05-24' ,'accepted'); 144 | 145 | CREATE TABLE all_users( 146 | user_id INT NOT NULL PRIMARY KEY, 147 | user_name CHAR(25), 148 | registration_date DATETIME, 149 | active_last_month BOOLEAN 150 | ); 151 | 152 | INSERT INTO all_users 153 | (user_id, user_name, registration_date, active_last_month) VALUES 154 | (1, 'sam', '2018-01-21', 1), 155 | (2, 'phelp', '2018-01-15', 1), 156 | (3, 'peyton', '2018-03-12', 1), 157 | (4, 'ryan', '2018-02-17', 0), 158 | (5, 'james', '2018-01-21', 0), 159 | (6, 'christine', '2018-02-27', 1), 160 | (7, 'bolt', '2018-02-28', 0), 161 | (8, 'jessica', '2018-01-11', 1), 162 | (9, 'paul', '2018-04-23', 1), 163 | (10, 'brian', '2018-03-12', 0); 164 | 165 | 166 | CREATE TABLE sport_accounts( 167 | sport_player_id INT, 168 | sport_player CHAR(25), 169 | sport_category CHAR(25), 170 | FOREIGN KEY (sport_player_id) 171 | REFERENCES all_users(user_id) 172 | ON DELETE CASCADE 173 | ); 174 | 175 | INSERT INTO sport_accounts 176 | (sport_player_id, sport_player, sport_category) VALUES 177 | (2, 'phelp', 'swimming'), 178 | (7, 'bolt', 'running'), 179 | (8,'jessica', 'swimming'), 180 | (9, 'paul', 'basketball'), 181 | (10, 'brian', 'cricket'), 182 | (5, 'james', 'cricket'); 183 | 184 | 185 | 186 | CREATE TABLE follow_relation( 187 | follower_id INT, 188 | target_id INT, 189 | following_date DATETIME, 190 | FOREIGN KEY (follower_id) 191 | REFERENCES all_users(user_id) 192 | ON DELETE CASCADE, 193 | FOREIGN KEY (target_id) 194 | REFERENCES all_users(user_id) 195 | ON DELETE CASCADE 196 | ); 197 | 198 | INSERT INTO follow_relation 199 | (follower_id, target_id, following_date) VALUES 200 | (1,8, '2018-01-02'), 201 | (5,2,'2018-01-02'), 202 | (9,10, '2018-01-02'), 203 | (10,8, '2018-01-02'), 204 | (8,3, '2018-01-02'), 205 | (4, 6, '2018-01-02'), 206 | (2,8, '2018-01-02'), 207 | (6,9, '2018-01-02'), 208 | (1,7, '2018-01-02'), 209 | (10,2, '2018-01-02'), 210 | (1,2, '2018-01-02'); 211 | 212 | CREATE TABLE ad_accounts( 213 | account_id INT, 214 | date DATETIME, 215 | account_status CHAR(15) 216 | ); 217 | 218 | INSERT INTO ad_accounts 219 | (account_id, date, account_status) VALUES 220 | (101, '2019-01-21', 'active'), 221 | (102, '2019-01-17', 'active'), 222 | (117, '2019-02-06', 'active'), 223 | (112, '2019-01-16', 'active'), 224 | (110, '2019-03-22', 'fraud'), 225 | (115, '2019-04-28', 'fraud'), 226 | (103, '2019-02-07', 'close'), 227 | (112, '2019-04-15', 'fraud'), 228 | (101, '2019-04-28', 'fraud'), 229 | (117, '2019-04-22', 'fraud'), 230 | (102, '2019-03-19', 'fraud'), 231 | (106, '2019-04-28', 'fraud'), 232 | (105, '2019-03-02', 'active'), 233 | (110, '2019-04-28', 'fraud'); 234 | 235 | CREATE TABLE user_details( 236 | date DATETIME, 237 | session_id INT, 238 | user_id INT 239 | ); 240 | 241 | CREATE TABLE event_session_details( 242 | date DATETIME, 243 | session_id INT, 244 | timespend_sec INT, 245 | user_id INT 246 | ); 247 | 248 | INSERT INTO user_details 249 | (date, session_id, user_id) values 250 | ('2019-01-10', 201, 6), 251 | ('2019-01-10', 202, 7), 252 | ('2019-01-10', 203, 6), 253 | ('2019-01-11', 204, 8), 254 | ('2019-01-10', 205, 6), 255 | ('2019-01-11', 206, 8), 256 | ('2019-01-12', 207, 9); 257 | 258 | INSERT INTO event_session_details 259 | (date, session_id, timespend_sec, user_id) VALUES 260 | ('2019-01-10', 201, 1200, 6), 261 | ('2019-01-10', 202, 100, 7), 262 | ('2019-01-10', 203, 1500, 6), 263 | ('2019-01-11', 204, 2000, 8), 264 | ('2019-01-10', 205, 1010, 6), 265 | ('2019-01-11', 206, 1780, 8), 266 | ('2019-01-12', 207, 2500, 9), 267 | ('2019-01-12', 208, 500, 9), 268 | ('2019-01-21', 209, 2798, 15), 269 | ('2019-01-25', 210, 1278, 18); 270 | 271 | CREATE TABLE messages_detail( 272 | user_id INT NOT NULL PRIMARY KEY, 273 | messages_sent INT, 274 | date DATE 275 | ); 276 | 277 | INSERT INTO messages_detail 278 | (user_id, messages_sent, date) VALUES 279 | (1, 120, '2014-04-27'), 280 | (2,50 , '2014-04-27'), 281 | (3, 222, '2014-04-27'), 282 | (4, 70, '2014-04-27'), 283 | (5, 250, '2014-04-27'), 284 | (6, 246,'2014-04-27'), 285 | (7, 179, '2014-04-27'), 286 | (8, 116, '2014-04-27'), 287 | (9, 84 , '2014-04-27'), 288 | (10, 215,'2014-04-27'), 289 | (11, 105, '2014-04-27'), 290 | (12, 174, '2014-04-27'), 291 | (13, 158, '2014-04-27'), 292 | (14, 30, '2014-04-27'), 293 | (15, 48, '2014-04-27'); 294 | 295 | CREATE TABLE user_name ( 296 | full_names CHAR(30) 297 | ); 298 | 299 | INSERT INTO user_name 300 | (full_names) VALUES 301 | ('Jessica Taylor'), 302 | ('Erin Russell'), 303 | ('Amanda Smith'), 304 | ('Sam Brown'), 305 | ('Robert Kehrer'); 306 | 307 | 308 | CREATE TABLE DIALOGLOG( 309 | user_id INT, 310 | app_id CHAR(5), 311 | type CHAR(15), 312 | date TIMESTAMP 313 | ); 314 | 315 | INSERT INTO DIALOGLOG 316 | (user_id, app_id, type, date) VALUES 317 | (1, 'a', 'impression', '2019-02-04'), 318 | (2, 'a', 'impression', '2019-02-04'), 319 | (2, 'a', 'click', '2019-02-04'), 320 | (3, 'b', 'impression', '2019-02-04'), 321 | (4, 'c', 'click', '2019-02-04'), 322 | (4, 'd', 'impression', '2019-02-04'), 323 | (5, 'd', 'click', '2019-02-04'), 324 | (6, 'd', 'impression', '2019-02-04'), 325 | (6, 'e', 'impression', '2019-02-04'), 326 | (3, 'a', 'impression', '2019-02-04'), 327 | (3, 'b', 'click', '2019-02-04'); 328 | 329 | CREATE TABLE friend_request( 330 | requestor_id INT, 331 | sent_to_id INT, 332 | time DATE 333 | ); 334 | 335 | INSERT INTO friend_request 336 | (requestor_id, sent_to_id, time) VALUES 337 | (1, 2, '2018-06-03'), 338 | (1, 3, '2018-06-08'), 339 | (1, 3, '2018-06-08'), 340 | (2, 4, '2018-06-09'), 341 | (3, 4, '2018-06-11'), 342 | (3, 5, '2018-06-11'), 343 | (3, 5, '2018-06-12'); 344 | 345 | CREATE TABLE request_accepted( 346 | acceptor_id INT, 347 | requestor_id INT, 348 | time DATE 349 | ); 350 | 351 | INSERT INTO request_accepted VALUES 352 | (2, 1, '2018-08-01'), 353 | (3, 1, '2018-08-01'), 354 | (3, 1, '2018-08-01'), 355 | (4, 2, '2018-08-02'), 356 | (5, 3, '2018-08-03'), 357 | (5, 3, '2018-08-03'), 358 | (5, 3, '2018-08-04'); 359 | 360 | CREATE TABLE new_request_accepted( 361 | acceptor_id INT, 362 | requestor_id INT, 363 | accept_date DATE 364 | ); 365 | 366 | INSERT INTO new_request_accepted 367 | (acceptor_id, requestor_id, accept_date) Values 368 | (2, 1, '2018-05-01'), 369 | (3, 1, '2018-05-02'), 370 | (4, 2, '2018-05-02'), 371 | (5, 3, '2018-05-03'), 372 | (3, 4, '2018-05-04'); 373 | 374 | CREATE TABLE count_request( 375 | country_code CHAR(10), 376 | count_of_requests_sent INT, 377 | percent_of_request_sent_failed CHAR(10), 378 | sent_date DATE 379 | ); 380 | 381 | INSERT INTO count_request 382 | (country_code, count_of_requests_sent, percent_of_request_sent_failed, sent_date) VALUES 383 | ('AU', 23676, '5.2%', '2018-09-07'), 384 | ('NZ', 12714, '2.1%', '2018-09-08'), 385 | ('IN', 24545, '4.6%', '2018-09-09'), 386 | ('IN', 34353, '5.3%', '2018-09-10'), 387 | ('AU', 24255, '1.7%', '2018-09-11'), 388 | ('NZ', 23131, '2.9%', '2018-09-12'), 389 | ('US', 49894, '5.3%','2018-09-13'), 390 | ('IN', 19374, '2.4%', '2018-09-14'), 391 | ('AU', 18485, '2.7%','2018-09-15'), 392 | ('IN', 38364, '3.5%', '2018-09-16'); 393 | 394 | 395 | CREATE TABLE confirmed_no( 396 | phone_number CHAR(15) 397 | ); 398 | 399 | INSERT INTO confirmation_no 400 | (phone_number) VALUES 401 | ('232-473-3433'), 402 | ('545-038-2294'), 403 | ('647-294-1837'), 404 | ('492-485-9727'), 405 | ('545-383-7837'), 406 | ('184-483-9575'), 407 | ('493-420-4902'), 408 | ('282-595-8373'), 409 | ('594-9594-2948'); 410 | 411 | INSERT INTO confirmed_no 412 | (phone_number) VALUES 413 | ('492-485-9727'), 414 | ('545-383-7837'), 415 | ('184-483-9575'), 416 | ('493-420-4902'); 417 | 418 | CREATE TABLE user_interaction( 419 | user_1 CHAR(5), 420 | user_2 CHAR(5), 421 | date DATE 422 | ); 423 | 424 | INSERT INTO user_interaction 425 | (user_1, user_2, date) VALUES 426 | ('A', 'B', '2019-03-23'), 427 | ('A', 'C', '2019-03-23'), 428 | ('B', 'D', '2019-03-23'), 429 | ('B', 'F', '2019-03-23'), 430 | ('C', 'D', '2019-03-23'), 431 | ('A', 'D', '2019-03-23'), 432 | ('B','C', '2019-03-23'), 433 | ('A','E', '2019-03-23'); 434 | 435 | create table salesperson( 436 | id INT, 437 | name CHAR(25), 438 | age INT, 439 | salary INT 440 | ); 441 | 442 | insert into salesperson 443 | (id, name, age, salary) values 444 | (1, 'Abe', 61, 140000), 445 | (2, 'Bob', 34, 44000), 446 | (5, 'Chris', 34, 40000), 447 | (7, 'Dan', 41, 52000), 448 | (8, 'Ken', 57, 115000), 449 | (11, 'Joe', 38, 38000); 450 | 451 | create table customer( 452 | id INT, 453 | name char(25), 454 | city char(25), 455 | industry_type char(1) 456 | ); 457 | 458 | insert into customer 459 | (id, name, city, industry_type) values 460 | (4, 'Samsonic', 'pleasant', 'J'), 461 | (6, 'Panasung', 'oaktown', 'J'), 462 | (7, 'Samsony', 'jackson', 'B'), 463 | (9, 'Orange', 'jackson', 'B'); 464 | 465 | create table orders( 466 | number int, 467 | order_date date, 468 | cust_id int, 469 | salesperson_id int, 470 | amount int 471 | ); 472 | 473 | insert into orders 474 | (number, order_date, cust_id, salesperson_id, amount) values 475 | (10, '1996-02-08', 4, 2, 540), 476 | (20, '1999-01-30', 4, 8, 1800), 477 | (30, '1995-07-14', 9, 1, 460), 478 | (40, '1998-01-29', 7, 2, 2400), 479 | (50, '1998-02-03', 6, 7, 600), 480 | (60, '1998-03-02', 6, 7, 720), 481 | (70, '1998-05-06', 6, 7, 150); 482 | 483 | create table event_log( 484 | user_id INT, 485 | event_date_time INT #Using plain INT column type to store unix timestamp is the most trivial option. 486 | ); 487 | 488 | Insert into event_log 489 | (user_id, event_date_time) values 490 | (7494212, 1535308430), 491 | (7494212, 1535308433), 492 | (1475185, 1535308444), 493 | (6946725, 1535308475), 494 | (6946725, 1535308476), 495 | (6946725, 1535308477); 496 | -------------------------------------------------------------------------------- /queries.sql: -------------------------------------------------------------------------------- 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 count in desc order. 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 FullName column of user_name table. 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 managers 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; 33 | 34 | select * from employee where employee_id in (select employee_ref_id from bonus where employee.employee_id = bonus.employee_ref_id); 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 from employee table 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 500000; 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 and month(joining_date) = 1; 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.salary and e1.employee_id != e2.employee_id; 58 | 59 | -- 15> Write a SQL query to show all departments along with the number of people working there. 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 group by department order by max(salary); 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 employee e2 WHERE e1.Salary <= e2.Salary) order by e1.Salary desc; 82 | 83 | -- 21> Write a SQL query to fetch departments along with the total salaries paid for each of them. 84 | select department, sum(salary) as 'Total Salary' from employee group by department order by sum(salary); 85 | 86 | -- 22> Write a SQL query to find employee with highest salary in an organization from employee table. 87 | select first_name, last_name from employee where salary = (select max(salary) from employee); 88 | 89 | -- 23> Write an SQL query that makes recommendations using the pages that your friends liked. 90 | -- Assume you have two tables: a two-column table of users and their friends, and a two-column table of 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) with highest 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 from attendance_events and all_students tables? 106 | select * from all_students; 107 | select * from attendance_events; 108 | 109 | select (count(attendance_events.student_id) * 100 / (select count(student_id) from attendance_events)) as Percent 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 all seven days 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 date from user_action table. 129 | select * from user_action; 130 | 131 | select count(a.user_id_who_sent)*100 / (select count(user_id_who_sent) from user_action) as percent 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 | 137 | -- 29> How many total users follow sport accounts from tables all_users, sport_accounts, follow_relation? 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 account_status= "active") as 'percent' 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 and event_session_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 | 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_detail table. 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_name table 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' or 'impression'. 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 through rate)' 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 of requests. 222 | -- Overall acceptate rate of requests = total number of acceptance / total number of requests. 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) as A) 228 | / 229 | (select count(*) from (select distinct requestor_id, sent_to_id from friend_request ) as B), 2),0 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 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 of requests sent failed 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_request_sent_failed 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 Total_percent_of_request_sent_failed 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 populated by volume in each bucket 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, 2) 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 2013-03-23 date 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 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 samsonic from 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 order with Samsonic from the table: salesperson, customer, orders 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, action), 321 | -- write a sql query that returns the name, phone number and most recent date for any user that has logged in 322 | -- over the last 30 days 323 | -- (you can tell a user has logged in if action field in UserHistory is set to 'logged_on') 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, action), 333 | -- Write a SQL query to determine which user_ids in the User table are not contained in the UserHistory table 334 | -- (assume the UserHistory table has a subset of the user_ids in User table). Do not use the SQL MINUS statement. 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 the maximum value 343 | -- from the numbers without using 344 | -- sql aggregate like max or min 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 than 2000 images in their presentations from event_log table 352 | -- There is a startup company that makes an online presentation software and they have event_log table that records every time a user inserted 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; 367 | --------------------------------------------------------------------------------