My Solution using MYSQL Workbench: -- Part-1 Solution: select Department, salary,Emp from ( select d.DepartmentName as Department, e.FirstName as Emp, e.salary as Salary, dense_rank() over(partition by d.DepartmentName order by salary desc) as rnk from employees e join departments d on e.DepartmentID = d.DepartmentID ) as rnk_table where rnk<=3; -- Part 2 Solution: SELECT AVG(Salary) AS AverageSalary FROM Employees WHERE DateHired >= DATE_SUB(CURDATE(), INTERVAL 5 YEAR); -------- Part 3 Solution: SELECT * FROM Employees WHERE Salary < ( SELECT AVG(Salary) FROM Employees WHERE DateHired >= DATE_SUB(CURDATE(), INTERVAL 5 YEAR) );
@datadecoder_sai3 күн бұрын
The following are my solutions in MS SQL: --Question 1 select authorName,count(BookID) as 'Books Written' from authors au join books b on au.AuthorID=b.AuthorID group by authorName --Question 2 select au.AuthorID,au.AuthorName ,b.BookID,b.BookTitle,b.PublicationYear from authors au join books b on au.AuthorID=b.AuthorID where year(PublicationYear)=year(getdate())-1
@DeviprasadSwain-gu3kr9 күн бұрын
Thank you for uploading such questions.I am benefitted by these questions.
@madhusudhanreddyt283813 күн бұрын
IN SQL SERVER, -- How to find the highest salary? select MAX(Salary) as Highest_Salary from Employees_Data; select TOP 1 Salary as Highest_Salary from Employees_Data order by Salary DESC; select distinct Salary as HighestSalary from ( select ID, FirstName+', '+LastName as EmployeeName, Gender, Salary, DENSE_RANK() OVER (ORDER BY Salary DESC) as drnk from Employees_Data ) as temp where temp.drnk = 1 -- How to find 2nd highest salary? select MAX(Salary) from Employees_Data where Salary < (select MAX(Salary) from Employees_Data) select top 1 Salary from ( select distinct TOP 2 Salary from Employees_Data order by Salary DESC ) as temp order by Salary asc; select distinct Salary as HighestSalary from ( select ID, FirstName+', '+LastName as EmployeeName, Gender, Salary, DENSE_RANK() OVER (ORDER BY Salary DESC) as drnk from Employees_Data ) as temp where temp.drnk = 2 -- How to find nth highest salary using a Co-Related Sub-Query? select * from Employees_Data as e1 where 2 = (select COUNT(distinct Salary) from Employees_Data as e2 where e2.Salary >= e1.Salary)
@madhusudhanreddyt283813 күн бұрын
IN SQL SERVER, -- with pivot operator select month as MonthName, [Electronics], [Clothing] from ( select month, category, amount from sales_data ) as sourcedata PIVOT ( SUM(amount) FOR category IN ([Electronics], [Clothing] ) ) as pivoteddata; -- without pivot operator select month as MonthName, SUM(CASE WHEN category = 'Electronics' THEN amount ELSE NULL END) as Electronics, SUM(CASE WHEN category = 'Clothing' THEN amount ELSE NULL END) as Clothing from sales_data group by month; thanks for the table script...saves a lot of time and makes my job bit easier :)
@madhusudhanreddyt283813 күн бұрын
in sql server, for 2nd question ;with cte_employees as ( select EmployeeID, FirstName+' '+LastName as EmployeeName, Salary, DateHired from Employees ) select AVG(Salary) as Average_Salary from cte_employees where DateHired >= DATEADD(YEAR, -5, GETDATE());
@sapshorts957216 күн бұрын
Thanks for your explanation. At video time (7:54), i mean while generating odd numbers from 1 to 10. We are getting 11 also, how to ignore it?
@sapshorts957216 күн бұрын
Hi Sir, Thanks for your explanation. I have doubt in Nth highest salary question. Why are we using order by twice in outer query. Then regarding offset, at first we gave 0 to find first high salary, then we need to use offset value 1 to find second highest salary right. Why you are using 2 to find second highest salary. If understand wrongly, please correct me.
@ಅಜಯಕನ್ನಡಿಗ-ಪ4ಫ25 күн бұрын
thank you sir for explaining step by step😊😊
@NafisAnsari-vr2xq25 күн бұрын
Great Video, Just went through your channel and the idea of tackling interview questions like this is great. Thanks, you just got a sub💯
@sambasivaraonelluriАй бұрын
1 st question answer: WITH RankedEmployees AS ( SELECT employee_id, salary, department, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank FROM employee_data ) SELECT employee_id, salary, department FROM RankedEmployees WHERE rank <= 3 ORDER BY department, rank;
@skilltechath0nАй бұрын
@sambasivaraonelluri Great! Keep learning :)
@sambasivaraonelluriАй бұрын
MY answer 2 qst :SELECT AVG(salary) AS avg_salary_hired_last_5_years FROM employee_data WHERE hire_year BETWEEN 2019 AND 2024;
@skilltechath0nАй бұрын
@sambasivaraonelluri Nice. Keep practicing :)
@apurvjadhav7995Ай бұрын
with cte as( select *,rank() over (partition by student_name order by marks desc) as rn from students) select student_name, sum(case when rn <=2 then marks else 0 end) as marks from cte group by student_name;
@skilltechath0nАй бұрын
@apurvjadhav7995 Great! Keep learning :)
@santoshjadhav7458Ай бұрын
These questions asked for 2- 3 years experience candidate or fresher
@skilltechath0nАй бұрын
@santoshjadhav7458 Yes. It can be also be asked to freshers as well! Depends upon the interviewer. I hope it helps :)
@affanskm3530Ай бұрын
WITH RECURSIVE CTE AS ( SELECT 1 AS order_id UNION SELECT order_id+1 FROM CTE WHERE order_id<20 ) SELECT order_id FROM CTE WHERE order_id NOT IN ( SELECT order_id FROM orders )
@skilltechath0nАй бұрын
@affanskm3530 Great! Keep practicing :)
@vaibhavshirbhate7561Ай бұрын
Why we use left join for 2nd question in weak in use of join cases so explain in detail please
@manishkambleofficialАй бұрын
Zoom in on Important Things Is Good Feature, Use it in Videos while editing
@RAHULEDITZSАй бұрын
MY ANSWER FOR 1ST QUE AND TQ BRO select distinct(EE.Salary),concat(EE.FirstName,'',EE.LastName) as Full_name,DD.DepartmentName FROM department DD Join employee EE on DD.DepartmentID=EE.DepartmentID group by concat(EE.FirstName,'',EE.LastName),DD.DepartmentName,EE.Salary order by DD.DepartmentName, EE.Salary DESC LIMIT 9;
@skilltechath0nАй бұрын
@RAHULEDITZS Great! Keep learning :)
@dhinakaranr9691Ай бұрын
Can we use where year(publicationyear) = Adddate(curdate(), interval -1, year) in the where condition? Please correct me if am wrong
@beedalabharathi3120Ай бұрын
Very useful for practice scenarios
@skilltechath0nАй бұрын
Glad it helps!
@prabuubarp3840Ай бұрын
i completed postgresql recently, i don't know how to practice and where to implement?
@skilltechath0nАй бұрын
You can use pgadmin tool to practice postgresql and solve the sql questions in our video for practice.
@bhargavimandala1886Ай бұрын
Good explanation
@skilltechath0nАй бұрын
Glad it was helpful!
@bhargavimandala1886Ай бұрын
This query I am using Select p.productName From product p Left join orders o on p.productid =o.productid Where o.orderid is null But I am getting no rows returned
@skilltechath0nАй бұрын
Will check and let you know.
@pravinshinde1891Ай бұрын
nice job keep it up kindly update more queryyyyyy
@skilltechath0nАй бұрын
Thank you! More SQL problems coming soon!
@pravinshinde1891Ай бұрын
nice explanation keep posted new one to help crack the interview
@skilltechath0nАй бұрын
Thank you, I will
@Shivya648Ай бұрын
Why didn't you take a.author id= b.book id?
@Pandimoori_krishАй бұрын
Which ide you using for sql
@skilltechath0nАй бұрын
I’m using Pgadmin for postgresql. Hope it helps 👍
@sweetsubha514Ай бұрын
Nicely explained 😃👍👍👌👌👌👌👌
@skilltechath0nАй бұрын
Appreciate the support! Keep practicing your SQL skills.
@chinmaymishra5086Ай бұрын
Instead of dense rank, we could use row number function, which helps better
@skilltechath0nАй бұрын
Great suggestion, using row_number() can also be a solution.
@shripaddeshpande5766Ай бұрын
I think no, row number will give salary in 3rd row that may not be 3rd highest salary. Dense rank is correct.
@Pandimoori_krishАй бұрын
Great job bro keep it up
@skilltechath0nАй бұрын
Thank you for the appreciation. Keep practicing!
@RAHULKUMAR-px8emАй бұрын
select P.ProductName,sum(O.Quantity * P.ProductPrice) as Total_Revenue from Products P left join Orders1 O on O.OrderID = P.ProductID group by P.ProductName Use Multiply bro for Qunatity * ProductPrice
@amitkumarkumarsinghpiyush323Ай бұрын
Please upload more query 🎉🎉
@skilltechath0nАй бұрын
Sure. Thank you for watching :)
@amitkumarkumarsinghpiyush323Ай бұрын
Ye question daily use me hote hai...?
@skilltechath0nАй бұрын
Yes. It's pretty common.
@amitkumarkumarsinghpiyush323Ай бұрын
Bro aap output ki jagah questions likh diya kro jo new h unke liye direct output smjhna thoda tough h
@skilltechath0nАй бұрын
@amitkumarkumarsinghpiyush323 Sure. Will make a note of it. Thank you for the suggestion :)
@Frelek321Ай бұрын
can u provide scripts for table and insert -> CHECK COMMENT
@skilltechath0nАй бұрын
@Frelek321 Thank you for the suggestion. I have added the query to create table and insert values in the description. I hope it helps. Keep practicing :)
@akashdongre7040Ай бұрын
with final as (select coalesce(order_id - lag(order_id) over (partition by id order by id),0) as diff , row_number() over (partition by id order by id) as row_num from input_table) select row_num as Order_id from final where diff=2
@skilltechath0nАй бұрын
@akashdongre7040 Great! Keep practicing :)
@ishanshubham83552 ай бұрын
select month, sum(if(category="electronics",amount,null)) as electronics, sum(if(category="clothing",amount,null)) as clothing from sales_data group by 1 order by 1;
@skilltechath0n2 ай бұрын
@ishanshubham8355 Great! Keep practicing :) Let us know if you need us to solve any particular question.
@ishanshubham83552 ай бұрын
select student_name, sum(marks) as marks from ( select *, dense_rank() over(partition by student_name order by marks desc) as rnk from students ) as m where rnk <3 group by 1
@skilltechath0n2 ай бұрын
@ishanshubham8355 Continue practicing, Wish you all the success for your endeavours :)
@RaviSankar-ln3ki2 ай бұрын
Informative.
@skilltechath0n2 ай бұрын
@RaviSankar-ln3ki Glad it was helpful!
@ishanshubham83552 ай бұрын
WITH RECURSIVE CTE AS ( SELECT MIN(ORDER_ID) AS cnt FROM orders UNION SELECT cnt+1 FROM cte WHERE cnt < (SELECT max(order_id) FROM orders) ) SELECT cnt AS order_id FROM cte WHERE cnt NOT IN (SELECT order_id FROM orders)
@skilltechath0n2 ай бұрын
@ishanshubham8355 Great! Keep practicing :)
@devarajululanka64272 ай бұрын
select month, sum(case when category = 'clothing' then amount end )as clothing, sum(case when category = 'electronics' then amount end )as electronics from sales_data group by month
@skilltechath0n2 ай бұрын
@devarajululanka6427 Great! Keep practicing :) Let us know if you need us to solve any particular question.
@chandramohan-bo5se2 ай бұрын
SELECT * FROM SALES_DATA SELECT MONTH,MAX(CASE WHEN CATEGORY='ELECTRONICS'THEN AMOUNT END) AS ELECTRONICS ,MAX(CASE WHEN CATEGORY='CLOTHING' THEN AMOUNT END) AS CLOTHING FROM SALES_DATA GROUP BY MONTH ORDER BY MONTH
@skilltechath0n2 ай бұрын
@chandramohan-bo5se Great! Keep practicing :) Let us know if you need us to solve any particular question.
@saravanansaran35752 ай бұрын
plss dnt stop making videos
@skilltechath0n2 ай бұрын
Thank you 🙏 Will make sure to upload videos at regular interval.
@varmakvm48122 ай бұрын
Hi, In first question while finding the top 3 employees within each department, dense_rank() would be appropriate one because it will handle tie values without skipping the ranking rather than row_number IMO. Thank you for the constant motivation through SQL Questions ! Looking for more ...
@skilltechath0n2 ай бұрын
Yes, definitely if the interviewer asked us to provide the same rank for duplicate values. Thanks for mentioning it 👍
@parthchauhan93052 ай бұрын
On the contrary, using dense_rank may give us more than 3 values (if there are ties), rather we only need to find top 3 highest paid employees (3 rows), row_number will always ensure that we get only 3 rows in the result set.
@tsidesh54802 ай бұрын
Without using crosstab, can do with group by, will the company accept it
@skilltechath0n2 ай бұрын
Yes, how you interpret the problem and solve, it's completely upto you. Everyone has a different way of perceiving the problem and solving it.