Posts

Query to find out the cities where covid cases are increasing continuously.

---Write a query to find out the cities where covid cases are increasing continuously.  create table covid(city varchar(50),days date,cases int); delete from covid; insert into covid values('DELHI','2022-01-01',100); insert into covid values('DELHI','2022-01-02',200); insert into covid values('DELHI','2022-01-03',300); insert into covid values('MUMBAI','2022-01-01',100); insert into covid values('MUMBAI','2022-01-02',100); insert into covid values('MUMBAI','2022-01-03',300); insert into covid values('CHENNAI','2022-01-01',100); insert into covid values('CHENNAI','2022-01-02',200); insert into covid values('CHENNAI','2022-01-03',150); insert into covid values('BANGALORE','2022-01-01',100); insert into covid values('BANGALORE','2022-01-02',300); insert into covid values('BANGALORE','2022-01-03',200)...

INDEXING in SQLSERVER

    What is INDEXING in SQL? 📢   ✔️ Indexes are special lookup tables that the database search engine can use to speed up data retrieval. ✔️ An index in a database is very similar to an index in the back of a book. For example, if you want to reference all pages in a book that discusses a certain topic, you first refer to the index, which lists all the topics alphabetically and are then referred to one or more specific page numbers. ✔️ An index helps to speed up SELECT queries and WHERE clauses, but it slows down data input, with the UPDATE and the INSERT statements. 🔎 When should Indexes be created? - A column contains a wide range of values, and table has millions of rows. - A column does not contain a large number of null values. - One or more columns are frequently used together in a where clause or a join condition. 🔎 When should indexes be avoided? - The table is small. - The columns are not often used as a condition in the query. - The column is ...

ETL vs ELT

  ETL vs ELT ⚡ ETL is a traditional process for data transformation into a data warehouse (Cloud based or On-prem). ELT is a modern process for any kind of structured and non-structured data transformation inside a cloud based data warehouse. ⚡ In ETL, data moves from source to stage, then into data warehouse. In ELT, data staging is not required. Data can directly move into data warehouse from source. ⚡ ETL only transforms and loads the Data which you think is necessary when creating DWH. Hence Data migration is slower. ELT loads all the Data immediately into DWH and users can decide later which Data to transform and analyze. Hence Data migration is faster. ⚡ ETL is not normally a solution for Data lakes. It transforms data for integration with structured and relational DWH. ELT offers a pipeline for Data lakes to ingest any kind of structured or unstructured data. Data could be transformed on need basis for analysis. ⚡ ETL is best suited for dealing with smaller datasets. ELT i...

SQL Query TO FIND THE AVERAGE PRICE OF EACH PRODUCT -Intreview Question

Write a SQL Query TO FIND THE AVERAGE PRICE OF EACH PRODUCT  -------------------START-----------------------------------------------------------------------   CREATE TABLE Price ( product_id INT NOT NULL, start_date DATE, end_date DATE, price INT ) insert Price values (1,GETDATE()-90,GETDATE()-70,100) insert Price values (1,GETDATE()-300,GETDATE()-100,120) insert Price values (2,GETDATE()-400,GETDATE()-300,220) insert Price values (2,GETDATE()-600,GETDATE()-500,320) SELECT * FROM Price SELECT * FROM Unitsold CREATE TABLE Unitsold ( product_id INT NOT NULL, purchase_date DATE, units INT) insert  Unitsold values(1,GETDATE()-80,22) insert  Unitsold values(1,GETDATE()-200,30) insert  Unitsold values(2,GETDATE()-340,40) insert  Unitsold values(2,GETDATE()-540,50) SELECT * FROM Price SELECT * FROM Unitsold ---------------------------------------------------SOLUTION--------------------------------- with CTE_1 as( select product_id,units,units*price as product_pric...

SQL Query who all are present inside the hospital-

 ---------- Write a SQL Query who all are present inside the hospital--- -------------------------------------- create table hospital ( emp_id int , action varchar(10) , time datetime); insert into hospital values ('1', 'in', '2019-12-22 09:00:00'); insert into hospital values ('1', 'out', '2019-12-22 09:15:00'); insert into hospital values ('2', 'in', '2019-12-22 09:00:00'); insert into hospital values ('2', 'out', '2019-12-22 09:15:00'); insert into hospital values ('2', 'in', '2019-12-22 09:30:00'); insert into hospital values ('3', 'out', '2019-12-22 09:00:00'); insert into hospital values ('3', 'in', '2019-12-22 09:15:00'); insert into hospital values ('3', 'out', '2019-12-22 09:30:00'); insert into hospital values ('3', 'in', '2019-12-22 09:45:00'); insert into hospit...

-Pareto Principle (80-20 Rule) Implementation in SQL||SQL INTREVIEW QUESTION

------------- --Pareto Principle (80-20 Rule) Implementation in SQL- ------------------------------- ---------------START--------------------------------------------------------------- with CTE1 as ( select SSO.Product_ID,SUM (SSO.Sales) as 'Product Sales' FROM  [dbo].[Sample - Superstore_Orders] SSO GROUP BY SSO.Product_ID ),Sales_cal as ( select Product_ID,Sales,sum (Sales) over (ORDER BY Sales DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 0 PRECEDING ) as 'Running Sales',SUM (Sales) OVER ()* 0.8 as"Total Sales" FROM  [dbo].[Sample - Superstore_Orders] ) select * from Sales_cal WHERE Sales_cal.[Running Sales] <=Sales_cal.[Total Sales] ------END----------------------------------------------------------

Product base SQL intreview question

----- ----Write a query to find PersonID,Name,Number of friends, and sum of marks of person who have friends with total score greater then 100 ----------- -------------------START---------------------------------------------------------------------------- select * from [ Person_Table] SELECT * FROM [dbo].[Friend] WITH CTE1 AS ( SELECT A.PersonID, count (A.PersonID) as "No of friends", SUM(B.Score) as "Total Marks" FROM [dbo].[Friend] A INNER JOIN [ Person_Table] B ON A.FriendID = B.PersonID group by  A.PersonID Having SUM(B.Score) >100 ) SELECT P.*,CTE1.[No of friends],CTE1.[Total Marks] FROM [ Person_Table] P INNER JOIN CTE1 ON CTE1.PersonID=P.PersonID --------------END----------------------------------------------------------------------------------------------------