Posts

How to retrieve data for more than 2 tables in SQL?

 DML Script:  -- Create Departments table CREATE TABLE Departments (     DepartmentID VARCHAR(10) PRIMARY KEY,     DepartmentName VARCHAR(50) ); -- Create Employees table CREATE TABLE Employees (     EmployeeID INT PRIMARY KEY,     Name VARCHAR(50),     DepartmentID VARCHAR(10)  ); -- Create Projects table CREATE TABLE Projects (     ProjectID VARCHAR(10) PRIMARY KEY,     ProjectName VARCHAR(50),     DepartmentID VARCHAR(10)  ); -- Insert data into Departments table INSERT INTO Departments (DepartmentID, DepartmentName) VALUES ('D001', 'HR'), ('D002', 'Finance'), ('D003', 'IT'); -- Insert data into Employees table INSERT INTO Employees (EmployeeID, Name, DepartmentID) VALUES (1, 'John Smith', 'D001'), (2, 'Jane Doe', 'D002'), (3, 'Sam Brown', 'D001'), (4, 'Linda White', 'D003'); -- Insert data into Projects table INSERT INTO Projects (ProjectID, ProjectName, DepartmentID) VAL...

Last person to enter the elevator | SQL Interview question

 DML Script: DROP TABLE IF EXISTS ElevatorOrder; GO CREATE TABLE  ElevatorOrder ( LineOrder  INTEGER PRIMARY KEY, [Name]     VARCHAR(100) NOT NULL, [Weight]   INTEGER NOT NULL ); GO INSERT INTO ElevatorOrder ([Name], [Weight], LineOrder) VALUES ('Haruto',611,1),('Minato',533,2),('Haruki',623,3), ('Sota',569,4),('Aoto',610,5),('Hinata',525,6); GO SELECT * FROM ElevatorOrder

Add end date to a balance amount table | SQL interview question

 DML Script: DROP TABLE IF EXISTS Balances; GO CREATE TABLE Balances ( CustomerID   INTEGER, BalanceDate  DATE, Amount       MONEY NOT NULL, PRIMARY KEY (CustomerID, BalanceDate) ); GO INSERT INTO  Balances (CustomerID, BalanceDate, Amount) VALUES (1001,'10/11/2021',54.32), (1001,'10/10/2021',17.65), (1001,'9/18/2021',65.56), (1001,'9/12/2021',56.23), (1001,'9/1/2021',42.12), (2002,'10/15/2021',46.52), (2002,'10/13/2021',7.65), (2002,'9/15/2021',75.12), (2002,'9/10/2021',47.34), (2002,'9/2/2021',11.11); GO SELECT * FROM Balances

Cancellation rate of a trips app using SQL | SQL Joins based scenario question

 DML Script: DROP TABLE IF EXISTS Trips; DROP TABLE IF EXISTS Users; Create table Trips (id int, client_id int, driver_id int, city_id int, status varchar(50), request_at varchar(50)) Create table Users (users_id int, banned varchar(50), role varchar(50))   insert into Trips (id, client_id, driver_id, city_id, status, request_at) values ('1', '1', '10', '1', 'completed', '2013-10-01') insert into Trips (id, client_id, driver_id, city_id, status, request_at) values ('2', '2', '11', '1', 'cancelled_by_driver', '2013-10-01') insert into Trips (id, client_id, driver_id, city_id, status, request_at) values ('3', '3', '12', '6', 'completed', '2013-10-01') insert into Trips (id, client_id, driver_id, city_id, status, request_at) values ('4', '4', '13', '6', 'cancelled_by_client', '2013-10-01') insert int...

Process logs condition setup using SQL | SQL Interview question

Logic:  If all steps of a workflow are of the same status (Error, Complete, or Running), then return the  distinct status.   If any steps of a workflow have an Error status along with a status of Complete or Running, set  the overall status to Indeterminate.   If the workflow steps have a combination of Complete and Running (without any Errors), set the  overall status to Running.  DML Script: DROP TABLE IF EXISTS  ProcessLog; GO CREATE TABLE  ProcessLog ( Workflow    VARCHAR(100), StepNumber  INTEGER, RunStatus   VARCHAR(100) NOT NULL  ); GO INSERT INTO  ProcessLog (Workflow, StepNumber, RunStatus) VALUES ('Alpha',1,'Error'),('Alpha',2,'Complete'),('Alpha',3,'Running'), ('Bravo',1,'Complete'),('Bravo',2,'Complete'), ('Charlie',1,'Running'),('Charlie',2,'Running'), ('Delta',1,'Error'),('Delta',2,'Error'), ('Echo',1,'Running'),(...

Running totals without over() clause | SQL interview question

 DML Script: DROP TABLE IF EXISTS  ProcessLog; GO CREATE TABLE ProcessLog ( Workflow       VARCHAR(100), ExecutionDate  DATE  ); GO INSERT INTO ProcessLog (Workflow, ExecutionDate) VALUES ('Alpha','6/01/2018'),('Alpha','6/14/2018'),('Alpha','6/15/2018'), ('Bravo','6/1/2018'),('Bravo','6/2/2018'),('Bravo','6/19/2018'), ('Charlie','6/1/2018'),('Charlie','6/15/2018'),('Charlie','6/30/2018'); GO

Human Traffic of a stadium | SQL Interview question

 DML Script: drop table If Exists Stadium Create table  Stadium (id int, visit_date DATE NULL, people int)   insert into Stadium (id, visit_date, people) values ('1', '2017-01-01', '10') insert into Stadium (id, visit_date, people) values ('2', '2017-01-02', '109') insert into Stadium (id, visit_date, people) values ('3', '2017-01-03', '150') insert into Stadium (id, visit_date, people) values ('4', '2017-01-04', '99') insert into Stadium (id, visit_date, people) values ('5', '2017-01-05', '145') insert into Stadium (id, visit_date, people) values ('6', '2017-01-06', '14') insert into Stadium (id, visit_date, people) values ('7', '2017-01-07', '199') insert into Stadium (id, visit_date, people) values ('8', '2017-01-08', '188') insert into Stadium (id, visit_date, people) values ('9', ...