Posts

Advanced SQL Techniques: Master the ROLLUP, CUBE AND GROUPINSETS Function

 DROP TABLE IF EXISTS Sales -- Step 1: Create the Sales Table CREATE TABLE Sales (     Product VARCHAR(50),     Region VARCHAR(50),     Sales INT ); -- Step 2: Insert Sample Data into the Sales Table INSERT INTO Sales (Product, Region, Sales) VALUES ('A', 'East', 100); INSERT INTO Sales (Product, Region, Sales) VALUES ('A', 'West', 150); INSERT INTO Sales (Product, Region, Sales) VALUES ('B', 'East', 200); INSERT INTO Sales (Product, Region, Sales) VALUES ('B', 'West', 250); select * from sales

SQL INTERVIEW QUESTION | Calculate 90th discrete percentile of each department

DROP TABLE IF EXISTS salaries  -- Create the salaries table CREATE TABLE salaries (     employee_id INT,     department VARCHAR(1),     salary INT ); -- Insert data into the salaries table INSERT INTO salaries (employee_id, department, salary) VALUES (1, 'A', 3000), (2, 'A', 5000), (3, 'A', 7000), (4, 'B', 2000), (5, 'B', 4000), (6, 'B', 6000), (7, 'C', 1000), (8, 'C', 3000), (9, 'C', 5000);

ORACLE INTERVIEW QUESTION | How to Assign Row Numbers Without ROW_NUMBER() Function

 -- Create the employees table DROP TABLE IF EXISTS employees; CREATE TABLE employees (     department_id INT,     employee_id INT,     name VARCHAR(50) ); -- Insert sample data into the employees table INSERT INTO employees (department_id, employee_id, name) VALUES (1, 1, 'Anna'), (1, 2, 'Ben'), (2, 3, 'Charlie'), (2, 4, 'David'), (1, 5, 'Eva'), (3, 6, 'George'), (3, 7, 'John'); SELECT * FROM employees

SQL INTERVIEW QUESTION | Join 3 Tables & Filter Records in 2 Tables Only

--query to join three tables and filter the results to show only records that exist in exactly two of the tables.  CREATE TABLE TableA (     ID INT,     ValueA VARCHAR(50) ); CREATE TABLE TableB (     ID INT,     ValueB VARCHAR(50) );   CREATE TABLE TableC (     ID INT,     ValueC VARCHAR(50) );  TRUNCATE TABLE TableA  TRUNCATE TABLE TableB  TRUNCATE TABLE TableC INSERT INTO TableA (ID, ValueA) VALUES (1, 'CommonValue'), (2, 'CommonValue'), (3, 'UniqueA'),(5,'CommonValue'); INSERT INTO TableB (ID, ValueB) VALUES (2, 'CommonValue'), (3, 'CommonValue'), (4, 'UniqueB'),(5,'CommonValue'); INSERT INTO TableC (ID, ValueC) VALUES (3, 'CommonValue'), (4, 'CommonValue'), (2, 'UniqueC'),(5,'CommonValue');

Calculate 90th continuous percentile of each region | SQL

 DROP TABLE IF EXISTS Sales CREATE TABLE Sales (       Region VARCHAR(50),     Revenue DECIMAL(10, 2) ); INSERT INTO Sales ( Region, Revenue) VALUES   (  'North', 1000.00), (  'North', 800.00), (  'North', 1200.00), (  'South', 1500.00), ( 'South', 2000.00), ( 'South', 1700.00),   ( 'East', 1100.00), (  'East', 900.00), ( 'East', 1300.00), (  'East', 1150.00),   ( 'West', 1400.00), (  'West', 1600.00), (  'West', 1350.00), ( 'West', 1500.00) ; --Number of rows (n) = 4 --Desired percentile (P) = 0.9 --Position in the ordered set (k) = (n - 1) * P + 1 = (4 - 1) * 0.9 + 1 = 3.7 --The position k falls between the 3rd and 4th values (1150 and 1300). --Using linear interpolation: --Percentile Value = prerow+(k-mod(k))*(currentrow-prerow) --Percentile Value = 1150+(3.7−3)×(1300−1150) --Percentile Value=1255

EY INTERVIEW QUESTION | Categorize Salespersons by Sales Amount in SQL

 -- Drop the table if it already exists DROP TABLE IF EXISTS sales; -- Create the sales table CREATE TABLE sales (     salesperson VARCHAR(50),     amount INT ); -- Insert data into the sales table INSERT INTO sales (salesperson, amount) VALUES ('Alice', 200), ('Alice', 300), ('Alice', 400), ('Alice', 500), ('Alice', 600), ('Bob', 150), ('Bob', 250), ('Bob', 350), ('Bob', 450), ('Bob', 550); Select * from sales

ADOBE SQL INTERVIEW QUESTION | Divide Employees into Buckets

 -- Create the employees table DROP TABLE IF EXISTS employees CREATE TABLE employees (     employee_id INT PRIMARY KEY,     salary INT ); -- Insert data into the employees table INSERT INTO employees (employee_id, salary) VALUES (1, 3000); INSERT INTO employees (employee_id, salary) VALUES (2, 2500); INSERT INTO employees (employee_id, salary) VALUES (3, 4000); INSERT INTO employees (employee_id, salary) VALUES (4, 5000); INSERT INTO employees (employee_id, salary) VALUES (5, 3500); INSERT INTO employees (employee_id, salary) VALUES (6, 2000); INSERT INTO employees (employee_id, salary) VALUES (7, 4500); INSERT INTO employees (employee_id, salary) VALUES (8, 6000); INSERT INTO employees (employee_id, salary) VALUES (9, 7000);  SELECT * FROM employees