- Видео 36
- Просмотров 52 808
Code-Con
Добавлен 8 окт 2023
Your one stop solution for your coding problems and building concepts.
WALAMART Data Engineer Interview Question
WALAMART Data Engineer Interview Question based on SQL Joins, Aggregate Functions and Window Functions.
DDL Commands :-
-- Create the employees table
drop table if exists empl
CREATE TABLE empl (
emp_id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);
-- Create the sales table
truncate table sls
CREATE TABLE sls (
emp_id INT,
dept VARCHAR(50),
sales DECIMAL(10, 2),
FOREIGN KEY (emp_id) REFERENCES employees(emp_id)
);
-- Insert sample data into employees table
INSERT INTO empl (emp_id, name, age) VALUES
(1, 'Alice', 28),
(2, 'Bob', 35),
(3, 'Charlie', 30),
(4, 'Diana', 25),
(5,'Mary',22);
-- Insert sample data into sales table
INSERT INTO sls (emp_id, dept, sales) VALUES
(1, 'Electronics', 1500.00),
(2, 'Furnit...
DDL Commands :-
-- Create the employees table
drop table if exists empl
CREATE TABLE empl (
emp_id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);
-- Create the sales table
truncate table sls
CREATE TABLE sls (
emp_id INT,
dept VARCHAR(50),
sales DECIMAL(10, 2),
FOREIGN KEY (emp_id) REFERENCES employees(emp_id)
);
-- Insert sample data into employees table
INSERT INTO empl (emp_id, name, age) VALUES
(1, 'Alice', 28),
(2, 'Bob', 35),
(3, 'Charlie', 30),
(4, 'Diana', 25),
(5,'Mary',22);
-- Insert sample data into sales table
INSERT INTO sls (emp_id, dept, sales) VALUES
(1, 'Electronics', 1500.00),
(2, 'Furnit...
Просмотров: 104
Видео
WALMART Interview Question
Просмотров 86Месяц назад
This question was asked in WALAMRT to find user's latest transaction and the number of orders placed by each user on that date. Below is the DDL of the question for reference. CREATE TABLE transactions ( product_id INT, user_id INT, spend DECIMAL(10, 2), transaction_date DATETIME ); INSERT INTO transactions (product_id, user_id, spend, transaction_date) VALUES (3673, 123, 68.90, '2022-07-08 10:...
SQL Interview Question | Using Lead Function
Просмотров 1322 месяца назад
Question :- Write a query to find customers who made exactly two purchases , and those purchases were made on two consecutive days . Script :- CREATE TABLE purchases ( id INT PRIMARY KEY, customer_id INT, purchase_date DATE, amount DECIMAL(10, 2) ); Insert sample data INSERT INTO purchases (id, customer_id, purchase_date, amount) VALUES (1, 101, '2023-09-01', 500), (2, 101, '2023-09-02', 300), ...
PayPal SQL Interview Question
Просмотров 1,4 тыс.3 месяца назад
Question - Write a query to obtain a list of departments with an average salary lower than the overall average salary of the company. However, when calculating the company's average salary, you must exclude the salaries of the department you are comparing it with. drop table if exists emp create table emp( emp_id int, emp_name varchar(20), department_id int, salary int, manager_id int, emp_age ...
SQL String Interview Question | Interview Question 22
Просмотров 2654 месяца назад
Hey guys here with a string sql interview question. Do give it a try and share it in comments. SQL Script : INSERT INTO labemp (ID, Name, Designation) VALUES (1, 'ABC', 'Scientist'), (2, 'PQR', 'Engineer'), (3, 'STU', 'Analyst'); select * from labemp; #dataanalytics #sql #dataengineer #amazon #meta #facebook #placement
SQL Interview Question | SQL Intermediate Question 22
Просмотров 2684 месяца назад
Question - Return schools and classes with atleast one student enrolled in Maths and Physics. DDL :- drop table if exists students CREATE TABLE Students ( StudentID INT NOT NULL PRIMARY KEY, School VARCHAR(100) NOT NULL, Class VARCHAR(50) NOT NULL ); CREATE TABLE Enrollments ( StudentID INT NOT NULL, Subject VARCHAR(100) NOT NULL ); INSERT INTO Students (StudentID, School, Class) VALUES (1, 'Gr...
Complex SQL Interview Question | Based on joins, aggregate functions and more
Просмотров 4436 месяцев назад
Hey all, up with a tricky yet interesting sql interview question. Give it a try. Question- Number of times each student appeared for the exams. DDL Query: CREATE TABLE Examinations ( student_id INT, subject_name VARCHAR(50) ); Insert data into Examinations table INSERT INTO Examinations (student_id, subject_name) VALUES (1, 'Math'), (1, 'Physics'), (1, 'Programming'), (2, 'Programming'), (1, 'P...
IBM INTERVEW QUESTIO | SQL Intermediate Question 22
Просмотров 4766 месяцев назад
Question Find the maximum number of offers each family can avail. Condition the family size should fall in the range of the countries. CREATE TABLE FAMILIES ( ID VARCHAR(50), NAME VARCHAR(50), FAMILY_SIZE INT ); Insert data into FAMILIES table INSERT INTO FAMILIES (ID, NAME, FAMILY_SIZE) VALUES ('c00dac11bde74750b4d207b9c182a85f', 'Alex Thomas', 9), ('eb6f2d3426694667ae3e79d6274114a4', 'Chris G...
SQL Interview Question | Intermediate SQL Question 21
Просмотров 4106 месяцев назад
Question - Find number of members in a team for each employee. drop table Employee_Team CREATE TABLE Employee_Team ( employee_id INT, team_id INT ); INSERT INTO Employee_Team (employee_id, team_id) VALUES (1, 8), (2, 8), (3, 8), (4, 7), (5, Null), (6, 9); select * from Employee_Team . . . . . . #amazon #ai #meta #facebook #instagram #netflix #dataanalytics #sql #placement #college
TRICKY SQL Interview Question | SQL Intermediate Question 20
Просмотров 1,2 тыс.6 месяцев назад
Question - Write an SQL query 𝐭𝐨 𝐟𝐢𝐧𝐝 𝐭𝐡𝐞 𝐭𝐱𝐧𝐦𝐨𝐧𝐭𝐡 𝐰𝐡𝐢𝐜𝐡 𝐡𝐚𝐬 𝐭𝐡𝐞 𝐦𝐚𝐱𝐢𝐦𝐮𝐦 𝐭𝐱𝐧𝐚𝐦𝐨𝐮𝐧𝐭 The approach is to arrange the data vertically so that applying aggerate functions becomes easy. Comment below yours approach too. create table eshop(txnmonth varchar(50),clothing int,electronics int,sports int); insert into eshop values('Jan',2000,1500,3000); insert into eshop values('Feb',1000,2500,4000); inser...
ONE SHOT Concept on SQL JOINS
Просмотров 1398 месяцев назад
This video covers the practical aspects of one of the most asked topis in interviews i.e. JOINS. Here i have implemented all the five joins in SQL and explained them. Do like, share and subscribe. #sqldeveloper #sql #dataanalytics #dataengineering #placement #college #interviewquestions #sqlinterviewquestionsandanswers #facebook #amazon #flipkart #myntra #tcs #deloittejobs #accenture
DELOITTE SQL Interview Question | SQL Intermediate Question 19
Просмотров 6 тыс.8 месяцев назад
Question - Find the products whose sales are increasing every year. CREATE TABLE products ( product_id INT PRIMARY KEY, product_name VARCHAR(50), category VARCHAR(50) ); INSERT INTO products (product_id, product_name, category) VALUES (1, 'Laptops', 'Electronics'), (2, 'Jeans', 'Clothing'), (3, 'Chairs', 'Home Appliances'); CREATE TABLE sales ( product_id INT, year INT, total_sales_revenue DECI...
Tricky SQL Question | SQL Intermediate Question 18
Просмотров 4498 месяцев назад
This is a tricky sql question where we need to map the children with their respective parents. ddl commands :- create table people(id int primary key not null, name varchar(20), gender char(2)); create table relations(c_id int, p_id int, FOREIGN KEY (c_id) REFERENCES people(id), foreign key (p_id) references people(id) ); insert into people (id, name, gender) values (107,'Days','F'), (145,'Hawb...
SQL Interview Question | SQL Intermediate Question
Просмотров 4489 месяцев назад
Question - Write an SQL query 𝐭𝐨 𝐟𝐢𝐧𝐝 𝐭𝐡𝐞 𝐜𝐨𝐮𝐧𝐭 𝐨𝐟 𝐝𝐢𝐬𝐭𝐢𝐧𝐜𝐭 𝐝𝐞𝐩𝐚𝐫𝐭𝐦𝐞𝐧𝐭𝐬 𝐢𝐧𝐜𝐥𝐮𝐝𝐢𝐧𝐠 𝐍𝐔𝐋𝐋. drop table department create table department(deptid int, deptname varchar(50)); insert into department values(1,'Tech'); insert into department values(2,'HR'); insert into department values(3,null); insert into department values(4,'Tech'); insert into department values(5,'HR'); select * from department; #da...
GOOGLE SQL Interview Question | SQL Intermediate Question 16
Просмотров 3959 месяцев назад
Write a query to calculate the sum of odd-numbered and even-numbered measurements separately for a particular day and display the results in two different columns. Refer to the Example Output below for the desired format. Definition: Within a day, measurements taken at 1st, 3rd, and 5th times are considered odd-numbered measurements, and measurements taken at 2nd, 4th, and 6th times are conside...
MICROSOFT SQL Interview Question | SQL Intermediate Question 15
Просмотров 3709 месяцев назад
MICROSOFT SQL Interview Question | SQL Intermediate Question 15
PWC Interview Question for Data Analyst Role | SQL Intermediate Question 14
Просмотров 61110 месяцев назад
PWC Interview Question for Data Analyst Role | SQL Intermediate Question 14
LeetCode SQL Interview Question | Find Second Highest Salary | Part 2
Просмотров 25510 месяцев назад
LeetCode SQL Interview Question | Find Second Highest Salary | Part 2
FAANG SQL Interview Question | SQL Intermediate Question 13
Просмотров 1,4 тыс.10 месяцев назад
FAANG SQL Interview Question | SQL Intermediate Question 13
L &T Infotech SQL Interview Question | SQL Intermediate Question 12
Просмотров 6 тыс.10 месяцев назад
L &T Infotech SQL Interview Question | SQL Intermediate Question 12
LEETCODE SQL Interview Question | PART 1
Просмотров 46510 месяцев назад
LEETCODE SQL Interview Question | PART 1
Find Transition Point using C++ | Amazon Interview Question
Просмотров 6111 месяцев назад
Find Transition Point using C | Amazon Interview Question
PWC Data Analyst Interview | SQL Intermediate Question 11
Просмотров 20 тыс.11 месяцев назад
PWC Data Analyst Interview | SQL Intermediate Question 11
AMAZON DATA ENGINEER Interview Question | SQL Intermediate Question 10
Просмотров 1,1 тыс.11 месяцев назад
AMAZON DATA ENGINEER Interview Question | SQL Intermediate Question 10
AMAZON Interview Question | SQL Intermediate Question 9
Просмотров 91011 месяцев назад
AMAZON Interview Question | SQL Intermediate Question 9
GOOGLE Interview Question | SQL Intermediate Question 8
Просмотров 25611 месяцев назад
GOOGLE Interview Question | SQL Intermediate Question 8
Complex Question on Recursive CTE | SQL Intermediate Question 7
Просмотров 554Год назад
Complex Question on Recursive CTE | SQL Intermediate Question 7
Recursive CTE | SQL Interview Question | Intermediate Level Concept
Просмотров 575Год назад
Recursive CTE | SQL Interview Question | Intermediate Level Concept
Google Interview Question | SQL Intermediate Question 6
Просмотров 2,2 тыс.Год назад
Google Interview Question | SQL Intermediate Question 6
Customer Retention and Churn Analysis | Part 2 | SQL Intermediate Question 6
Просмотров 726Год назад
Customer Retention and Churn Analysis | Part 2 | SQL Intermediate Question 6
audio not clr
with cte as ( select sales_date, sum(case when fruits = 'apples' then sold_num end) apples, sum(case when fruits = 'oranges' then sold_num end) oranges from fruits_sales group by Sales_date ) select sales_date, apples-oranges as diff from cte
here are my two diff solution to approach this problem - ** advance sol using window function ** with cte as (select *, count(purchase_date) over(partition by customer_id) as purchase_count, min(purchase_date) over(partition by customer_id) as purchase_start, max(purchase_date) over(partition by customer_id) as purchase_end, timestampdiff(day,min(purchase_date) over(partition by customer_id),max(purchase_date) over(partition by customer_id)) as diff from practice.purchases) select * from cte where purchase_count = 2 and diff = 1 ** Normal solution without window function ** select customer_id, count(1), min(purchase_date) as start_date, max(purchase_date) as end_date, timestampdiff(day,min(purchase_date),max(purchase_date)) as diff from practice.purchases group by customer_id having timestampdiff(day,min(purchase_date),max(purchase_date)) = 1 and count(1) = 2
My Approach select customer_id from purchases group by customer_id having count(purchase_Date)=2 and datediff(day,min(purchase_Date) , max(purchase_Date))=1
Good one bro
couldn't understand how datediff function addressed the lead null values as difference would be 1 for first row of each customer_id...what about the second row wherein we've null values
for the second row for each custmoer_id the datediff will print NULL itself.
WITH CTE AS ( select *,( purchase_date - lead ( purchase_date ) over ( partition by customer_id order by purchase_date )) as difference, ( amount + lead( amount ) over ( partition by customer_id order by purchase_date )) as total_sum from purchases ) select customer_id from cte where difference = -1;
select dep_id,max(salary), min(salary) from employee group by dep_id
With cte as( select *, min(salary) over(partition by dep_id order by dep_id) as min_salary, max(salary) over(partition by dep_id order by dep_id) as max_salary from employee) select distinct dep_id, min_salary, max_salary from cte
please find the below solution : with cte as(select a.product_id as pid,a.PRODUCT_NAME as pn,a.CATEGORY as c, case when (lead(b.TOTAL_SALES_REVENUE,1,0) over (order by a.product_id) > b.TOTAL_SALES_REVENUE and lead(b.TOTAL_SALES_REVENUE,2,0) over (order by a.product_id) > lead(b.TOTAL_SALES_REVENUE,1,0) over (order by a.product_id)) then a.product_id else null end as x from products a inner join sales b on a.product_id = b.product_id) select pid as product_id,pn as product_name,c as category from cte where x is not null;
Can u suggest where to learn SQL
select top 1 txnmonth, (clothing+electronics+sports) AS amount from eshop ORDER BY amount DESC;
Nice work Buddy🎉
My solution 😢😢😢😢😢😢😢
WITH CTE AS(SELECT p.*,S.year,S.total_sales_revenue, total_sales_revenue - LAG(S.total_sales_revenue,1,S.total_sales_revenue)OVER(pARTITION BY S.product_id ORDER BY S.YEAR) AS HIKE FROM products AS p LEFT JOIN SALES AS S ON p.product_id = S.product_id),V1 AS( SELECT *, CASE WHEN HIKE < 0 THEN 'D' ELSE 'Q' END AS STAT FROM CTE) SELECT product_id,product_name,category FROM V1 WHERE product_id NOT IN (SELECT product_id FROM V1 WHERE STAT = 'D') group by 1,2,3;
select Initcap(name)||'('||substr(designation,1,1)||')' as Output from labemp;
For how many years of experience was this question relevant?
with cte as( select *,iif(lag(total_sales_revenue,1,total_sales_revenue)over(Partition by product_id order by year)<=total_sales_revenue ,1,0) As isincreasing from #sales) select Distinct Product_id from cte a where isincreasing=all(select isincreasing from cte where product_id=a.product_id)
with cte as (select s.sid,'new in source' as comment from sources s left join targets t on s.sid=t.tid where t.tid is null union all select t.tid,'Mismatch' as comment from targets t join sources s on t.tid=s.sid where t.tname<>s.sname union all select t.tid,'new in Target' as comment from targets t left join sources s on t.tid=s.sid where s.sid is null ) select * from cte
with cte as ( select company_id,user_id from company_users where language in ('English','German') group by user_id,company_id having count(language)=2 ) select company_id FROM cte group by company_id having count(user_id)>=2;
my approach in mysql : with fathers as (select p.id,p.name as father_name,r.c_id from people p inner join relations r on p.id=r.p_id where p.gender='M'), mothers as ( select p.id,p.name as mother_name,r.c_id from people p inner join relations r on p.id=r.p_id where p.gender='F') select mother_name,father_name,pl.name as child_name from mothers m join fathers f on m.c_id=f.c_id join people pl on f.c_id=pl.id order by pl.name;
Here is my approach : with cte as (select team_id as team_id,count(1) as team_cnt from employee_team group by 1) select employee_id,ifnull(team_cnt,0) as team_size from cte c right join employee_team e on c.team_id=e.team_id;
not sure if i understood the problem correctly by the example but i think this works perfectly fine w/o multiple ctes: "SELECT TOP 1 txnmonth FROM eshop ORDER BY clothing + electronics + sports DESC"
--Find the company who have alteast 2 users who speaks both English and German. with cte as ( select company_id,USER_ID,language, DENSE_RANK()over(partition by USER_ID order by language)as rn, count(language)over(partition by USER_ID)as cnt from Google_Intermediate_Interview where language in ('English', 'German') ) select company_id,USER_ID,language from cte where cnt >= 2
My approach : with cte as (select *,lead(flag) over(partition by empd_id order by swipe) as next_flag, lead(swipe) over(partition by empd_id order by swipe) as next_log from clocked_hours) select empd_id,cast(sum(case when flag='I' and next_flag='O' then cast(next_log-swipe as time) else 0 end) as time) as total_clocked_hrs from cte group by 1;
Nice✌️
so much background noise.🤷♂🤷♂
will try to improve
WITH cte AS (select student_id, school, class, subject from students where subject IN ('maths','physics')) select school, class, COUNT(DISTINCT student_id) AS student_count from cte where student_id IN (select student_id from cte group by student_id having COUNT(subject) >=2) group by school, class having student_count >=1; This will be the answer
select * from products where product_id not in ( select distinct product_id from ( select *, coalesce(total_sales_revenue-lag(total_sales_revenue) over(partition by product_id order by year asc),0) as lag_col from sales)a where a.lag_col<0 )
We can create case statement as well right? If current destination > Prev destination then 1 else 0 .. And then filter the result where we have only 1 Kindly let me know if this works or not.. Many thanks ❤
Thanks for the video. I used the lag function and used distinct instead of max in the output line. Is it valid? Please check. with cte as ( select p.product_id, p.product_name, s.year, total_sales_revenue, LAG(total_sales_revenue,1) over (partition by p.product_id order by year) as prev_year_revenue from products p join sales s on p.product_id = s.product_id --order by p.product_id, s.year ) select distinct product_id, product_name from cte where product_id not in (select product_id from cte where total_sales_revenue < prev_year_revenue)
I used self join: select p.name, max(case when pp.gender = 'F' then pp.name end) Mother, max(case when pp.gender = 'M' then pp.name end) Father from people p join relations r on p.id = r.c_id join people pp on pp.id = r.p_id group by p.name Thanks for the video.
with cte as ( select empNo,eName,sal,deptno , max(sal) over (partition by deptno) as max_sal , min(sal) over (partition by deptno) as Min_sal from emp2) select c1.empNo,c1.eName,c1.deptno,c2.max_sal,c2.min_sal from cte c1 join cte c2 on c1.empno=c2.empno where c1.sal= c2.max_sal or c1.sal=c2.min_sal order by c1.deptno
Select taxmonth, sum(cast(clothing as int)+cast(electronics as int)+cast(sports as int)) as total_sales From eshop Group by taxmonth order by total_sales desc limit 1 this als works
other method----- select st.student_id ,st.student_name ,s.subject_name ,NULLif(count(e.subject_name),0) as No_of_times_appeared from Students st cross join Subjects s left join Examinations e on e.student_id = st.student_id and e.subject_name = s.subject_name group by st.student_id,st.student_name,s.subject_name order by st.student_id
select sales_date, sum(case when fruits='apples' then sold_num end) apple_count, sum(case when fruits='oranges' then sold_num end) orange_count, sum(case when fruits='apples' then sold_num end) - sum(case when fruits='oranges' then sold_num end) diff_count from sales group by sales_date
Are the two users 1 and 3 who are working in company 1? Does this correct?
yeah
Nice explanation,and good question though ✅
select [dep_id],max([salary]) as higest_salary, min([salary]) as lowest_salary from [Sambit].[dbo].[emp] group by [dep_id]
select distinct cust_id from (select * ,min(order_date) over(partition by cust_id order by order_date) as min_date ,max(order_date) over(partition by cust_id order by order_date desc) as max_date from transactions)a where (month(max_date)-month(min_date))=1
select * from( select sid , case when sname <> tname then 'Mimatched' when tid is null then 'New in sources' end Review from sources as s left join targets t on s.sid = t.tid) t1 where Review is not null union select tid , 'New in targets' from targets where tid not in( select sid from sources) is this correct solution?
copy past question and solution, why you do like this do some real good question
select txnmonth from eshop where clothing + electronics + sports = ( select max(clothing + electronics + sports) from eshop);
Thanks for your content. Can you please add below in description which would help create table merchant(merchant_id varchar(20) , amount int, payment_mode varchar(20)); insert into merchant values ('m1',200,'cash'),('m2',520,'online'),('m1',700,'online'),('m3',1400,'online'),('m2',50,'cash'),('m1',300,'cash'); select * from merchant;
select user_id,company_id from ( select count(rn) as cunt,user_id,company_id from ( select company_id,user_id,language,rank() over (partition by company_id order by user_id ) rn from company_users where language in('english','german') ) as a group by user_id,company_id ) as b where cunt >=2
SELECT D.dep_Name, MAX (salary) AS highest_salary, MIN(Salary) AS lowest_salary FROM EMployees E LEFT JOIN DEPARTMENTS D ON E.DEP_ID = D.DEPT_ID_DEP GROUP BY D.DEP_NAME Please let me know is it correct or wrong
with cte as (select *, lead(Fruits) over(partition by Sales_date order by sales_date) le_fu111, lead(sold_num) over(partition by Sales_date order by sales_date) le_fu11 from sales) select *, sold_num-le_fu11 from cte where le_fu11 is not null;
emp id, 4,7 are not 3rd highest salary
I got emp id--2, 6,9,10
with cte as ( select *, count(dep_id) over(partition by dep_id ) as dep_count, ROW_NUMBER() over(partition by dep_id order by emp_salary ) as rn from employees ) select * from cte where dep_count >=3 and rn=3 union select * from cte where dep_count <3 and rn=1 order by rn desc
Cant we do this Select taxmonth, Max(clothing+electronics+sports) From eshop Group by 1
no this is syntax wise all instead you can use this Select taxmonth, sum(cast(clothing as int)+cast(electronics as int)+cast(sports as int)) as total_sales From eshop Group by taxmonth order by total_sales desc limit 1