From 9b1e9eb64d731413f48da6dd18aa239d484f240c Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 27 Aug 2020 10:56:44 -0500 Subject: [PATCH 1/5] added postgres schema definition --- DB/postgres_schema.sql | 498 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 498 insertions(+) create mode 100644 DB/postgres_schema.sql diff --git a/DB/postgres_schema.sql b/DB/postgres_schema.sql new file mode 100644 index 0000000..482639b --- /dev/null +++ b/DB/postgres_schema.sql @@ -0,0 +1,498 @@ +CREATE SEQUENCE Employee_seq; + +CREATE TABLE Employee ( + EMPLOYEE_ID INT NOT NULL PRIMARY KEY DEFAULT NEXTVAL ('Employee_seq'), + FIRST_NAME CHAR(25), + LAST_NAME CHAR(25), + SALARY INT, + JOINING_DATE TIMESTAMP(0), + DEPARTMENT CHAR(25), + MANAGER_ID INT +); + +INSERT INTO Employee + (EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY, JOINING_DATE, DEPARTMENT,MANAGER_ID) VALUES + (001, 'James', 'Smith', 100000, '2000-02-17', 'HR', 002), + (002, 'Jessica', 'Kohl', 80000, '2011-06-17', 'Admin', 005), + (003, 'Alex', 'Garner', 300000, '2000-02-17', 'HR', 011), + (004, 'Pratik', 'Pandey', 500000, '2000-02-17', 'Admin', 020), + (005, 'Christine', 'Robinson', 500000, '2011-06-17', 'Admin', 007), + (006, 'Deepak', 'Gupta', 200000, '2011-06-17', 'Account', 015), + (007, 'Jennifer', 'Paul', 75000, '2000-01-17', 'Account', 012), + (008, 'Deepika', 'Sharma', 90000, '2011-04-17', 'Admin', 017); + +CREATE TABLE Bonus ( + EMPLOYEE_REF_ID INT, + BONUS_AMOUNT INT, + BONUS_DATE TIMESTAMP(0), + FOREIGN KEY (EMPLOYEE_REF_ID) + REFERENCES Employee(EMPLOYEE_ID) + ON DELETE CASCADE +); + +INSERT INTO Bonus + (EMPLOYEE_REF_ID, BONUS_AMOUNT, BONUS_DATE) VALUES + (001, 5000, '2000-02-20'), + (002, 3000, '2000-06-18'), + (003, 4000, '2000-02-18'), + (001, 4500, '2000-02-18'), + (002, 3500, '2011-06-18'); + +CREATE TABLE Title ( + EMPLOYEE_REF_ID INT, + EMPLOYEE_TITLE CHAR(25), + AFFECTED_FROM TIMESTAMP(0), + FOREIGN KEY (EMPLOYEE_REF_ID) + REFERENCES Employee(EMPLOYEE_ID) + ON DELETE CASCADE +); + +INSERT INTO Title + (EMPLOYEE_REF_ID, EMPLOYEE_TITLE, AFFECTED_FROM) VALUES + (001, 'Manager', '2018-02-20 00:00:00'), + (002, 'Executive', '2018-06-11 00:00:00'), + (008, 'Executive', '2018-06-11 00:00:00'), + (005, 'Manager', '2018-06-11 00:00:00'), + (004, 'Asst. Manager', '2018-06-11 00:00:00'), + (007, 'Executive', '2018-06-11 00:00:00'), + (006, 'Lead', '2018-06-11 00:00:00'), + (003, 'Lead', '2018-06-11 00:00:00'); + + + CREATE TABLE all_students ( + student_id INT NOT NULL PRIMARY KEY, + school_id INT, + grade_level INT, + date_of_birth TIMESTAMP(0), + hometown CHAR(25) + ); + CREATE TABLE attendance_events ( + date_event TIMESTAMP(0), + student_id INT, + attendance CHAR(20), + FOREIGN KEY (student_id) + REFERENCES all_students(student_id) + ON DELETE CASCADE + ); + + INSERT INTO all_students + (student_id, school_id, grade_level, date_of_birth, hometown) VALUES + (110111, 205, 1, '2013-01-10', 'Pleasanton'), + (110115, 205, 1, '2013-03-15', 'Dublin'), + (110119, 205, 2, '2012-02-13', 'San Ramon'), + (110121, 205, 3, '2011-01-13', 'Dublin'), + (110125, 205, 2, '2012-04-25','Dublin'), + (110129, 205, 3, '2011-02-22', 'San Ramon'); + + INSERT INTO attendance_events + (date_event, student_id, attendance) VALUES + ('2018-01-10', 110111, 'present'), + ('2018-01-10', 110121, 'present' ), + ('2018-01-12', 110115, 'absent'), + ('2018-01-13', 110119, 'absent'), + ('2018-01-13', 110121, 'present'), + ('2018-01-14', 110125, 'present'), + ('2018-02-05', 110111, 'absent'), + ('2018-02-17', 110115, 'present'), + ('2018-02-22', 110129, 'absent'); + + + + CREATE TABLE login_info ( + user_id INT, + login_time TIMESTAMP(0) + ); + + INSERT INTO login_info + (user_id, login_time) VALUES + (1, '2017-08-10 14:32:25'), + (2, '2017-08-11 14:32:25'), + (3, '2017-08-11 14:32:25'), + (2, '2017-08-13 14:32:25'), + (3, '2017-08-14 14:32:25'), + (4, '2017-08-15 14:32:25'), + (5, '2017-08-12 14:32:25'), + (2, '2017-08-18 14:32:25'), + (1, '2017-08-11 14:32:25'), + (1, '2017-08-12 14:32:25'), + (1, '2017-08-13 14:32:25'), + (1, '2017-08-14 14:32:25'), + (1, '2017-08-15 14:32:25'), + (1, '2017-08-16 14:32:25'), + (1, '2017-08-17 14:32:25'), + (3, '2017-08-20 14:32:25'), + (5, '2017-08-16 14:32:25'), + (2, '2017-08-21 14:32:25'), + (3, '2017-08-22 14:32:25'); + + CREATE TABLE USER_ACTION ( + user_id_who_sent INT, + user_id_to_whom INT, + date_action TIMESTAMP(0), + action CHAR(25) + ); + + INSERT INTO USER_ACTION + (user_id_who_sent, user_id_to_whom, date_action, action) VALUES + (20251, 28272, '2018-05-24','accepted'), + (19209, 64638,'2018-06-13' , 'sent'), + (43744, 16373, '2018-04-15' ,'accepted'), + (20251, 18171, '2018-05-19' , 'sent'), + (54875, 36363, '2018-01-11' ,'rejected'), + (38292, 16373, '2018-05-24','accepted'), + (19209, 26743, '2018-06-12' ,'accepted'), + (27623, 28272, '2018-05-24','accepted'), + (20251, 37378, '2018-03-17','rejected'), + (43744, 18171, '2018-05-24' ,'accepted'); + + CREATE TABLE all_users( + user_id INT NOT NULL PRIMARY KEY, + user_name CHAR(25), + registration_date TIMESTAMP(0), + active_last_month BOOLEAN + ); + + INSERT INTO all_users + (user_id, user_name, registration_date, active_last_month) VALUES + (1, 'sam', '2018-01-21', True), + (2, 'phelp', '2018-01-15', True), + (3, 'peyton', '2018-03-12', True), + (4, 'ryan', '2018-02-17', False), + (5, 'james', '2018-01-21', False), + (6, 'christine', '2018-02-27', False), + (7, 'bolt', '2018-02-28', False), + (8, 'jessica', '2018-01-11', False), + (9, 'paul', '2018-04-23', False), + (10, 'brian', '2018-03-12', False); + + + CREATE TABLE sport_accounts( + sport_player_id INT, + sport_player CHAR(25), + sport_category CHAR(25), + FOREIGN KEY (sport_player_id) + REFERENCES all_users(user_id) + ON DELETE CASCADE + ); + + INSERT INTO sport_accounts + (sport_player_id, sport_player, sport_category) VALUES + (2, 'phelp', 'swimming'), + (7, 'bolt', 'running'), + (8,'jessica', 'swimming'), + (9, 'paul', 'basketball'), + (10, 'brian', 'cricket'), + (5, 'james', 'cricket'); + + + + CREATE TABLE follow_relation( + follower_id INT, + target_id INT, + following_date TIMESTAMP(0), + FOREIGN KEY (follower_id) + REFERENCES all_users(user_id) + ON DELETE CASCADE, +FOREIGN KEY (target_id) + REFERENCES all_users(user_id) + ON DELETE CASCADE + ); + + INSERT INTO follow_relation + (follower_id, target_id, following_date) VALUES + (1,8, '2018-01-02'), + (5,2,'2018-01-02'), + (9,10, '2018-01-02'), + (10,8, '2018-01-02'), + (8,3, '2018-01-02'), + (4, 6, '2018-01-02'), + (2,8, '2018-01-02'), + (6,9, '2018-01-02'), + (1,7, '2018-01-02'), + (10,2, '2018-01-02'), + (1,2, '2018-01-02'); + + CREATE TABLE ad_accounts( + account_id INT, + date TIMESTAMP(0), + account_status CHAR(15) +); + +INSERT INTO ad_accounts +(account_id, date, account_status) VALUES +(101, '2019-01-21', 'active'), +(102, '2019-01-17', 'active'), +(117, '2019-02-06', 'active'), +(112, '2019-01-16', 'active'), +(110, '2019-03-22', 'fraud'), +(115, '2019-04-28', 'fraud'), +(103, '2019-02-07', 'close'), +(112, '2019-04-15', 'fraud'), +(101, '2019-04-28', 'fraud'), +(117, '2019-04-22', 'fraud'), +(102, '2019-03-19', 'fraud'), +(106, '2019-04-28', 'fraud'), +(105, '2019-03-02', 'active'), +(110, '2019-04-28', 'fraud'); + +CREATE TABLE user_details( +date TIMESTAMP(0), +session_id INT, +user_id INT +); + +CREATE TABLE event_session_details( +date TIMESTAMP(0), +session_id INT, +timespend_sec INT, +user_id INT +); + +INSERT INTO user_details +(date, session_id, user_id) values +('2019-01-10', 201, 6), +('2019-01-10', 202, 7), +('2019-01-10', 203, 6), +('2019-01-11', 204, 8), +('2019-01-10', 205, 6), +('2019-01-11', 206, 8), +('2019-01-12', 207, 9); + +INSERT INTO event_session_details +(date, session_id, timespend_sec, user_id) VALUES +('2019-01-10', 201, 1200, 6), +('2019-01-10', 202, 100, 7), +('2019-01-10', 203, 1500, 6), +('2019-01-11', 204, 2000, 8), +('2019-01-10', 205, 1010, 6), +('2019-01-11', 206, 1780, 8), +('2019-01-12', 207, 2500, 9), +('2019-01-12', 208, 500, 9), +('2019-01-21', 209, 2798, 15), +('2019-01-25', 210, 1278, 18); + +CREATE TABLE messages_detail( +user_id INT NOT NULL PRIMARY KEY, +messages_sent INT, +date DATE +); + +INSERT INTO messages_detail +(user_id, messages_sent, date) VALUES +(1, 120, '2014-04-27'), +(2,50 , '2014-04-27'), +(3, 222, '2014-04-27'), +(4, 70, '2014-04-27'), +(5, 250, '2014-04-27'), +(6, 246,'2014-04-27'), +(7, 179, '2014-04-27'), +(8, 116, '2014-04-27'), +(9, 84 , '2014-04-27'), +(10, 215,'2014-04-27'), +(11, 105, '2014-04-27'), +(12, 174, '2014-04-27'), +(13, 158, '2014-04-27'), +(14, 30, '2014-04-27'), +(15, 48, '2014-04-27'); + +CREATE TABLE user_name ( +full_names CHAR(30) +); + +INSERT INTO user_name +(full_names) VALUES +('Jessica Taylor'), +('Erin Russell'), +('Amanda Smith'), +('Sam Brown'), +('Robert Kehrer'); + + +CREATE TABLE DIALOGLOG( +user_id INT, +app_id CHAR(5), +type CHAR(15), +date TIMESTAMP(0) +); + +INSERT INTO DIALOGLOG +(user_id, app_id, type, date) VALUES +(1, 'a', 'impression', '2019-02-04'), +(2, 'a', 'impression', '2019-02-04'), +(2, 'a', 'click', '2019-02-04'), +(3, 'b', 'impression', '2019-02-04'), +(4, 'c', 'click', '2019-02-04'), +(4, 'd', 'impression', '2019-02-04'), +(5, 'd', 'click', '2019-02-04'), +(6, 'd', 'impression', '2019-02-04'), +(6, 'e', 'impression', '2019-02-04'), +(3, 'a', 'impression', '2019-02-04'), +(3, 'b', 'click', '2019-02-04'); + +CREATE TABLE friend_request( +requestor_id INT, +sent_to_id INT, +time DATE +); + +INSERT INTO friend_request +(requestor_id, sent_to_id, time) VALUES +(1, 2, '2018-06-03'), +(1, 3, '2018-06-08'), +(1, 3, '2018-06-08'), +(2, 4, '2018-06-09'), +(3, 4, '2018-06-11'), +(3, 5, '2018-06-11'), +(3, 5, '2018-06-12'); + +CREATE TABLE request_accepted( +acceptor_id INT, +requestor_id INT, +time DATE +); + +INSERT INTO request_accepted VALUES +(2, 1, '2018-08-01'), +(3, 1, '2018-08-01'), +(3, 1, '2018-08-01'), +(4, 2, '2018-08-02'), +(5, 3, '2018-08-03'), +(5, 3, '2018-08-03'), +(5, 3, '2018-08-04'); + +CREATE TABLE new_request_accepted( +acceptor_id INT, +requestor_id INT, +accept_date DATE +); + +INSERT INTO new_request_accepted +(acceptor_id, requestor_id, accept_date) Values +(2, 1, '2018-05-01'), +(3, 1, '2018-05-02'), +(4, 2, '2018-05-02'), +(5, 3, '2018-05-03'), +(3, 4, '2018-05-04'); + +CREATE TABLE count_request( +country_code CHAR(10), +count_of_requests_sent INT, +percent_of_request_sent_failed CHAR(10), +sent_date DATE +); + +INSERT INTO count_request +(country_code, count_of_requests_sent, percent_of_request_sent_failed, sent_date) VALUES +('AU', 23676, '5.2%', '2018-09-07'), +('NZ', 12714, '2.1%', '2018-09-08'), +('IN', 24545, '4.6%', '2018-09-09'), +('IN', 34353, '5.3%', '2018-09-10'), +('AU', 24255, '1.7%', '2018-09-11'), +('NZ', 23131, '2.9%', '2018-09-12'), +('US', 49894, '5.3%','2018-09-13'), +('IN', 19374, '2.4%', '2018-09-14'), +('AU', 18485, '2.7%','2018-09-15'), +('IN', 38364, '3.5%', '2018-09-16'); + + +CREATE TABLE confirmed_no( +phone_number CHAR(15) +); + +INSERT INTO confirmed_no +(phone_number) VALUES +('232-473-3433'), +('545-038-2294'), +('647-294-1837'), +('492-485-9727'), +('545-383-7837'), +('184-483-9575'), +('493-420-4902'), +('282-595-8373'), +('594-9594-2948'); + +INSERT INTO confirmed_no +(phone_number) VALUES +('492-485-9727'), +('545-383-7837'), +('184-483-9575'), +('493-420-4902'); + +CREATE TABLE user_interaction( +user_1 CHAR(5), +user_2 CHAR(5), +date DATE +); + +INSERT INTO user_interaction +(user_1, user_2, date) VALUES +('A', 'B', '2019-03-23'), +('A', 'C', '2019-03-23'), +('B', 'D', '2019-03-23'), +('B', 'F', '2019-03-23'), +('C', 'D', '2019-03-23'), +('A', 'D', '2019-03-23'), +('B','C', '2019-03-23'), +('A','E', '2019-03-23'); + +create table salesperson( +id INT, +name CHAR(25), +age INT, +salary INT +); + +insert into salesperson +(id, name, age, salary) values +(1, 'Abe', 61, 140000), +(2, 'Bob', 34, 44000), +(5, 'Chris', 34, 40000), +(7, 'Dan', 41, 52000), +(8, 'Ken', 57, 115000), +(11, 'Joe', 38, 38000); + +create table customer( +id INT, +name char(25), +city char(25), +industry_type char(1) +); + +insert into customer +(id, name, city, industry_type) values +(4, 'Samsonic', 'pleasant', 'J'), +(6, 'Panasung', 'oaktown', 'J'), +(7, 'Samsony', 'jackson', 'B'), +(9, 'Orange', 'jackson', 'B'); + +create table orders( +number int, +order_date date, +cust_id int, +salesperson_id int, +amount int +); + +insert into orders +(number, order_date, cust_id, salesperson_id, amount) values +(10, '1996-02-08', 4, 2, 540), +(20, '1999-01-30', 4, 8, 1800), +(30, '1995-07-14', 9, 1, 460), +(40, '1998-01-29', 7, 2, 2400), +(50, '1998-02-03', 6, 7, 600), +(60, '1998-03-02', 6, 7, 720), +(70, '1998-05-06', 6, 7, 150); + +create table event_log( +user_id INT, +event_date_time INT --Using plain INT column type to store unix timestamp is the most trivial option. +); + +Insert into event_log +(user_id, event_date_time) values +(7494212, 1535308430), +(7494212, 1535308433), +(1475185, 1535308444), +(6946725, 1535308475), +(6946725, 1535308476), +(6946725, 1535308477); From 7bf89e780a90f82b1ba20e261217f0fd7e5cdf30 Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 27 Aug 2020 10:57:31 -0500 Subject: [PATCH 2/5] Rename Schemas.sql to DB/msql_schema.sql --- Schemas.sql => DB/msql_schema.sql | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Schemas.sql => DB/msql_schema.sql (100%) diff --git a/Schemas.sql b/DB/msql_schema.sql similarity index 100% rename from Schemas.sql rename to DB/msql_schema.sql From 23ed6c4827e44a040d4353565ab6027a937c547d Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 27 Aug 2020 10:57:56 -0500 Subject: [PATCH 3/5] Rename msql_schema.sql to mysql_schema.sql --- DB/{msql_schema.sql => mysql_schema.sql} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename DB/{msql_schema.sql => mysql_schema.sql} (100%) diff --git a/DB/msql_schema.sql b/DB/mysql_schema.sql similarity index 100% rename from DB/msql_schema.sql rename to DB/mysql_schema.sql From 7f9dd9b8df2b29b8d58c6cb52e797dad5bba8e42 Mon Sep 17 00:00:00 2001 From: Varun Date: Sat, 17 Oct 2020 13:48:25 -0500 Subject: [PATCH 4/5] Create queries_v1 --- queries_v1 | 363 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 363 insertions(+) create mode 100644 queries_v1 diff --git a/queries_v1 b/queries_v1 new file mode 100644 index 0000000..c30eebd --- /dev/null +++ b/queries_v1 @@ -0,0 +1,363 @@ +--Write a SQL query to find the nth highest salary from employee table. +-- Example: finding 3rd highest salary from employee table +select * from employee e2 ; +select * from employee e order by salary desc offset 2 limit 1; + +select * from +(select *, dense_rank() over (order by salary desc) as rnk from employee e) as rnk_tbl +where rnk=3; + +SELECT * /*This is the outer query part */ +FROM Employee Emp1 +WHERE 2 = ( /* Subquery starts here */ +SELECT COUNT(DISTINCT(Emp2.Salary)) +FROM Employee Emp2 +WHERE Emp2.Salary > Emp1.Salary); + +-- 2> Write a SQL query to find top n records? +-- Example: finding top 5 records from employee table + +select * from employee e limit 5; + +-- 3> Write a SQL query to find the count of employees working in department 'Admin' + +select count(*) as no_of_emp_in_admin from employee e where department = 'Admin'; + +-- 4> Write a SQL query to fetch department wise count employees sorted by department count in desc order. + +select department, count(*) as employeecount from employee e group by department order by employeecount desc ; + +-- 5> Write a query to fetch only the first name(string before space) from the FullName column of user_name table. + +select * from user_name un limit 2; +select split_part(un.full_names, ' ', 1), full_names from user_name un ; + +-- 6> Write a SQL query to find all the employees from employee table who are also managers +select * from employee; +select e1.* from employee e1 +join employee e2 on e1.employee_id = e2.manager_id ; + +-- 7> Write a SQL query to find all employees who have bonus record in bonus table +select * from employee e +join bonus b on e.employee_id = b.employee_ref_id ; + +-- 8> Write a SQL query to find only odd rows from employee table +select * from +(select *,row_number () over() as rn from employee e) as tmp +where rn % 2= 1; + +-- 9> Write a SQL query to fetch first_name from employee table in upper case +select upper(first_name) from employee e ; + +-- 10> Write a SQL query to get combine name (first name and last name) of employees from employee table +select first_name || ' ' || last_name as employee_name from employee e ; + +-- 11> Write a SQL query to print details of employee of employee 'Jennifer' and 'James'. +select * from employee e where first_name in ('Jennifer', 'James'); + +-- 12> Write a SQL query to fetch records of employee whose salary lies between +select * from employee e where salary between 50000 and 90000; + +-- 13> Write a SQL query to get records of employe who have joined in Jan 2000 +select * from employee e2 ; +select * from employee e where extract (year from e.joining_date) = '2000' and extract (month from e.joining_date )='1'; + +-- 14> Write a SQL query to get the list of employees with the same salary +select e1.* +from employee e1, employee e2 +where e1.salary = e2.salary and e1.employee_id <> e2.employee_id ; + +-- 15> Write a SQL query to show all departments along with the number of people working there. +select department, count(*) as emp_count from employee e group by department; + +-- 16> Write a SQL query to show the last record from a table. +select * from employee where employee_id = (select max(employee_id) from employee); + +-- 17> Write a SQL query to show the first record from a table. +select * from employee where employee_id = (select min(employee_id) from employee); + +-- 18> Write a SQL query to get last five records from a employee table. +(select * from employee order by employee_id desc limit 5) order by employee_id; + +-- 19> Write a SQL query to find employees having the highest salary in each department. +select * from +(select *, dense_rank () over(partition by department order by salary desc) as rnk +from employee e2) as tmp +where rnk=1; + +-- 20> Write a SQL query to fetch three max salaries from employee table. +select * from +(select *, dense_rank() over (order by salary desc) as rnk from employee e) as rnk_tbl +where rnk<=3; + +-- 21> Write a SQL query to fetch departments along with the total salaries paid for each of them. +select department, sum(salary) from employee e2 group by department ; + +-- 22> Write a SQL query to find employee with highest salary in an organization from employee table. +select * from employee e2 where salary = (select max(salary) from employee e3 ); + +-- 23> Write an SQL query that makes recommendations using the pages that your friends liked. +-- Assume you have two tables: a two-column table of users and their friends, and a two-column table of +-- users and the pages they liked. It should not recommend pages you already like. + +-- 24> write a SQL query to find employee (first name, last name, department and bonus) with highest bonus. +select * from bonus b3 ; +select * from employee e +join bonus b on e.employee_id = b.employee_ref_id where b.bonus_amount = (select max(bonus_amount) from bonus b2) ; + +-- 25> write a SQL query to find employees with same salary +select * +from employee e1, employee e2 +where e1.employee_id <> e2.employee_id and e1.salary =e2.salary ; + +-- 26> Write SQL to find out what percent of students attend school on their birthday from attendance_events and all_students tables? +select * from attendance_events ae ; +select * from all_students as2 ; + +select count(*)*100.0/(select count(distinct student_id) from attendance_events ae2 ) from all_students as2 +join attendance_events ae on as2.student_id = ae .student_id +where extract(day from date_event) = extract(day from date_of_birth) and extract(month from date_event) = extract(month from date_of_birth) +and attendance = 'present'; + +-- 27> Given timestamps of logins, figure out how many people on Facebook were active all seven days +-- of a week on a mobile phone from login info table? +select * from login_info li3 ; +select li.user_id, li.login_time, li2.login_time, li.login_time + interval '7' day as wk from login_info li +join login_info li2 on li.user_id = li2.user_id and + + +li.login_time in all(li2.login_time + interval '1' day, li2.login_time + interval '2' day, li2.login_time + interval '3' day, li2.login_time + interval '4' day, li2.login_time + interval '5' day, li2.login_time + interval '6' day) + + +select * from login_info li3 ; + +SELECT li.user_id, + COUNT( DISTINCT DATE( li.login_time )) AS days +FROM login_info li +join login_info li2 on li.user_id = li2.user_id +WHERE li.login_time > li2.login_time - INTERVAL '2' DAY +GROUP BY li.user_id +HAVING COUNT( DISTINCT DATE( li.login_time )) >= 2; + +SELECT user_id, + COUNT( DISTINCT DATE( login_date )) AS days +FROM Log_Table +WHERE login_date > DATE_SUB( CURRENT_TIMESTAMP, INTERVAL 3 DAY ) +GROUP BY user_id +HAVING days >= 3; + +-- 28> Write a SQL query to find out the overall friend acceptance rate for a given date from user_action table. + INSERT INTO USER_ACTION + (user_id_who_sent, user_id_to_whom, date_action, action) VALUES + (20251, 28272, '2018-05-24','sent'), + (19209, 64638,'2018-06-13' , 'accepted') +select * from user_action ua ; + +select date_action, "action", count(*)*100.0/(select count(*) from user_action ua2 where ua2.date_action = ua.date_action) +from user_action ua +group by date_action, "action" +order by date_action + +-- 29> How many total users follow sport accounts from tables all_users, sport_accounts, follow_relation? +select * from all_users au ; +select * from sport_accounts sa ; +select * from follow_relation fr ; + + +select count(distinct c.follower_id) as count_all_sports_followers +from sport_accounts a +join all_users b +on a.sport_player_id = b.user_id +join follow_relation c +on b.user_id = c.target_id; + +-- 30> How many active users follow each type of sport? +select sa.sport_category , count(distinct au.user_id) +from all_users au +join follow_relation fr on au.user_id = fr.follower_id +join sport_accounts sa on fr.target_id = sa.sport_player_id +where au.active_last_month = true +group by sa.sport_category ; + +-- 31> What percent of active accounts are fraud from ad_accounts table? +select * from ad_accounts aa3 ; +select count(*)*100.0/(select count(*) from ad_accounts aa2 where aa2.account_status = 'active') +from ad_accounts aa where account_status = 'fraud' and account_status = 'active'; + +select count(distinct a.account_id)/(select count(account_id) from ad_accounts where account_status= "active") as 'percent' +from ad_accounts a +join ad_accounts b +on a.account_id = b.account_id +where a.account_status = 'fraud' and b.account_status='active'; + +-- 32> How many accounts became fraud today for the first time from ad_accounts table? + +select count(account_id) 'First time fraud accounts' from ( +select distinct a.account_id, count(a.account_status) +from ad_accounts a +join ad_accounts b +on a.account_id = b.account_id +where b.date = curdate() and a.account_status = 'fraud' +group by account_id +having count(a.account_status) = 1) ad_accnt; + +-- 33> Write a SQL query to determine avg time spent per user per day from user_details and event_session_details + +select * from user_details ud ; +select * from event_session_details esd ; + +select "date", user_id, sum(timespend_sec)/60.0 as avg_mins +from event_session_details esd +group by "date", user_id + +-- 34> write a SQL query to find top 10 users that sent the most messages from messages_detail table. +select * from messages_detail md ; +select user_id, sum(messages_sent) as total_msgs +from messages_detail md +group by user_id +order by total_msgs desc +limit 10; + +-- 35> Write a SQL query to find disctinct first name from full user name from usere_name table +select * from user_name un + +select distinct split_part(un.full_names, ' ', 1) from user_name un ; + +-- 36> You have a table with userID, appID, type and timestamp. type is either 'click' or 'impression'. +-- Calculate the click through rate from dialoglog table. Now do it in for each app. +-- click through rate is defined as (number of clicks)/(number of impressions) +select * from dialoglog; + +select app_id, COALESCE(sum(case when type = 'click' then 1 else 0 end)*1.0/nullif(sum(case when type = 'impression' then 1 else 0 end),0),0) as ctr +from dialoglog d2 +group by app_id; + +-- 37> Given two tables Friend_request (requestor_id, sent_to_id, time), +-- Request_accepted (acceptor_id, requestor_id, time). Find the overall acceptance rate of requests. +-- Overall acceptate rate of requests = total number of acceptance / total number of requests. +select * from friend_request fr ; +select * from request_accepted ra ; + +select (select count(acceptor_id) from request_accepted ra),(select count(sent_to_id) from friend_request fr) + +-- 38> from a table of new_request_accepted, find a user with the most friends. +select * from new_request_accepted nra ; + +select acceptor_id, count(requestor_id) +from new_request_accepted nra group by acceptor_id ; + +-- 39> from the table count_request, find total count of requests sent and total count of requests sent failed +-- per country + +select * from count_request cr ; + +select *, ROUND((COALESCE(replace(percent_of_request_sent_failed,'%',''),0::text)::numeric)*count_of_requests_sent /100,0) +from count_request cr + +select country_code, sum(count_of_requests_sent) as reqs_sent, sum(ROUND((COALESCE(replace(percent_of_request_sent_failed,'%',''),0::text)::numeric)*count_of_requests_sent /100,0)) as reqs_failed +from count_request cr +group by country_code + +-- 40> create a histogram of duration on x axis, no of users on y axis which is populated by volume in each bucket +-- from event_session_details +select * from event_session_details; + +select floor(timespend_sec/500)*500 as bucket, +count(distinct user_id) as count_of_users +from event_session_details +group by 1; + +-- 41> Write SQL query to calculate percentage of confirmed messages from two tables : +-- confirmation_no (phone numbers that facebook sends the confirmation messages to) and +-- confirmed_no (phone numbers that confirmed the verification) + +-- 42> Write SQL query to find number of users who had 4 or more than 4 interactions on 2013-03-23 date +-- from user_interaction table (user_1, user_2, date). +-- assume there is only one unique interaction between a pair of users per day + +select * from user_interaction ui + +select usr, sum(cnt) +from (select user_1 as usr, count(user_1) as cnt +from user_interaction ui +where "date" = '2019-03-23' +group by user_1 +union all +select user_2 as usr , count(user_2) as cnt +from user_interaction ui +where "date" = '2019-03-23' +group by user_2 ) as tmp +group by usr +having sum(cnt) >= 4 + +-- 43> write a sql query to find the names of all salesperson that have order with samsonic from +-- the table: salesperson, customer, orders + +select * from salesperson s ; +select * from customer c ; +select * from orders o ; + +select distinct s2."name" +from salesperson s2 +join orders o2 on s2.id = o2.salesperson_id +join customer c2 on o2.cust_id = c2.id +where c2."name" = 'Samsonic'; + +-- 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 +select * from salesperson s where id not in (select distinct s2.id +from salesperson s2 +join orders o2 on s2.id = o2.salesperson_id +join customer c2 on o2.cust_id = c2.id and c2."name" ='Samsonic'); + +-- 45> Wrie a sql query to find the names of salespeople that have 2 or more orders. +select s2."name" , count("number") as no_of_orders +from salesperson s2 +join orders o2 on s2.id = o2.salesperson_id +group by s2."name" +having count("number") >= 2 + +-- 46> Given two tables: User(user_id, name, phone_num) and UserHistory(user_id, date, action), +-- write a sql query that returns the name, phone number and most recent date for any user that has logged in +-- over the last 30 days +-- (you can tell a user has logged in if action field in UserHistory is set to 'logged_on') +select user.name, user.phone_num, max(userhistory.date) +from user,userhistory +where user.user_id = userhistory.user_id +and userhistory.action = 'logged_on' +and userhistory.date >= date_sub(curdate(), interval 30 day) +group by user.name; + +-- 47> Given two tables: User(user_id, name, phone_num) and UserHistory(user_id, date, action), +-- Write a SQL query to determine which user_ids in the User table are not contained in the UserHistory table +-- (assume the UserHistory table has a subset of the user_ids in User table). Do not use the SQL MINUS statement. +-- Note: the UserHistory table can have multiple entries for each user_id. +select user.user_id +from user +left join userhistory +on user.user_id = userhistory.user_id +where userhistory.user_id is null; +-- 48> from a given table compare(numbers int(4)), write a sql query that will return the maximum value +-- from the numbers without using +-- sql aggregate like max or min + +select numbers +from compare +order by numbers desc +limit 1; + +-- 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 +-- There is a startup company that makes an online presentation software and they have event_log table that records every time a user inserted +-- an image into a presentation. one user can insert multiple images +select * from event_log el + +select count(*) from +(select user_id, count(event_date_time) as image_per_user +from event_log +group by user_id) as image_per_user +where image_per_user <2000 and image_per_user>1000; + +-- 50> select the most recent login time by values from the login_info table + +select user_id, max(login_time) from login_info li +group by user_id +order by user_id From 3ceba390397e204ea60ad57c28ea8bdb5a1a41ea Mon Sep 17 00:00:00 2001 From: Varun Date: Sat, 17 Oct 2020 13:48:53 -0500 Subject: [PATCH 5/5] Rename queries_v1 to queries_v1.sql --- queries_v1 => queries_v1.sql | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename queries_v1 => queries_v1.sql (100%) diff --git a/queries_v1 b/queries_v1.sql similarity index 100% rename from queries_v1 rename to queries_v1.sql