--------------WELCOME TO INTELIPAAT---------------------
-------- GOOD MORNING AND GOOD EVENING ALL !!------------
--------- I'M KRANTHI , YOUR MSSQL TRAINER --------------
--1) AND,IN,OR & NOT OPERATORS
USE CREATIVE_cARS
SELECT * FROM CarSales
--AND
Requirment#1 : Hi Raja, please provide the CHICAGO cars list AND new york CARS LIST
in
the CARSALES_tABLES.
SELECT * FROM CarSales where WorkLocation = 'chicago';
SELECT * FROM CarSales where WorkLocation IN ('chicago','nEW YORK');
SELECT * FROM CarSales where WorkLocation IN ('chicago','nEW YORK','HOUSTON');
Hi Bhargav, please provide the CHICAGO cars list and 2016 year LIST in
the CARSALES_tABLES.
SELECT * FROM CarSales WHERE WorkLocation = 'CHICAGO' AND CARYEAR=2016;
SELECT * FROM CarSales WHERE WorkLocation = 'CHICAGO' AND CARYEAR=2025; -- ERROR
--QUESTION1: FILITERING COLUMNS?: SELECT COLUMNS ACCORDING
-- FILTERING ROWS/RECORDS : WHERE
SELECT * FROM CarSales WHERE CARYEAR=2016;
-- OR
SELECT * FROM CarSales WHERE WorkLocation = 'CHICAGO' OR CARYEAR=2016;
SELECT * FROM CarSales WHERE WorkLocation = 'CHICAGO' OR CARYEAR=2025;
SELECT * FROM CarSales WHERE WorkLocation = 'CHICAGO' OR CARMAKE='CHEVROLET';
--NOT
SELECT * fROM CarSales WHERE NOT CARYEAR = 2016
-- MULTI OPERATORES (AND IN)
SELECT * fROM CarSales WHERE CARYEAR = 2016 AND NOT CARMAKE='CHEVROLET';
---- UNION OPERATORS : TYPES: UNION, UNION ALL, EXCEPT, INTERSECT
--UNION : IT RETURNS THE OUTPUT OF TWO TABLES/MORE AND REMOVE DUPLICATES
CREATE TABLE Students_Details1 (
S_id INT PRIMARY KEY,
S_Name VARCHAR(50),
S_Marks INT
);
CREATE TABLE Students_Details2 (
S_Id INT PRIMARY KEY,
S_Name VARCHAR(50),
S_Marks INT
);
INSERT INTO Students_Details1 (s_id, S_name, S_marks) VALUES
(1, 'Sam', 45),
(2, 'Bob', 87),
(3, 'Anne', 73),
(4, 'Julia', 92);
INSERT INTO Students_Details2 (s_id, S_name, S_marks) VALUES
(3, 'Anne', 73),
(4, 'Julia', 92),
(5, 'Matt', 65);
--UNION
SELECT * fROM Students_Details1
UNION
SELECT * fROM Students_Details2
--UNION ALL
--UNION ALL: IT RETURNS THE OUTPUT OF TWO TABLES/MORE AND IT ALLOWS DUPLICATES;
SELECT * fROM Students_Details1
UNION ALL
SELECT * fROM Students_Details2
--EXCEPT: IT RETURNS DISTINCT ROWS FROM THE FIRST/LEFT SELECT QUERY
SELECT * fROM Students_Details1
EXCEPT
SELECT * fROM Students_Details2
SELECT * fROM Students_Details2
EXCEPT
SELECT * fROM Students_Details1
--INTERSECT
SELECT * fROM Students_Details1
INTERSECT
SELECT * fROM Students_Details2
---LIKE
CREATE TABLE GM_EMLPOYEE
(
EID INT,
ENAME VARCHAR(50),
ESALARY MONEY,
EAGE INT,
EGENDER VARCHAR(6),
EDEPT VARCHAR(25),
ERATINGS VARCHAR(25)
);
INSERT INTO GM_EMLPOYEE (EID, ENAME, ESALARY, EAGE, EGENDER, EDEPT, ERATINGS)
VALUES
(1, 'John Doe', 50000.00, 35, 'Male', 'IT', 'Excellent'),
(2, 'Jane Smith', 60000.00, 32, 'Female', 'HR', 'Good'),
(3, 'Bob Johnson', 75000.00, 31, 'Male', 'Finance', 'Excellent'),
(4, 'Alice Williams', 80000.00, 33, 'Female', 'Marketing', 'Average'),
(5, 'Charlie Brown', 55000.00, 37, 'Male', 'Sales', 'Good'),
(6, 'Diana Miller', 70000.00, 36, 'Female', 'IT', 'Excellent'),
(7, 'Edward Davis', 62000.00, 34, 'Male', 'HR', 'Good'),
(8, 'Fiona Clark', 68000.00, 38, 'Female', 'Finance', 'Excellent'),
(9, 'George Wilson', 53000.00, 35, 'Male', 'Marketing', 'Average'),
(10, 'Helen Taylor', 72000.00, 33, 'Female', 'Sales', 'Good'),
(11, 'Isaac Turner', 62000.00, 28, 'Male', 'IT', 'Excellent'),
(12, 'Jessica Carter', 55000.00, 22, 'Female', 'HR', 'Good'),
(13, 'Kevin White', 68000.00, 30, 'Male', 'Finance', 'Excellent'),
(14, 'Lily Morgan', 50000.00, 24, 'Female', 'Marketing', 'Average'),
(15, 'Michael Walker', 72000.00, 26, 'Male', 'Sales', 'Good'),
(16, 'Natalie Hall', 60000.00, 29, 'Female', 'IT', 'Excellent'),
(17, 'Oliver Davis', 67000.00, 23, 'Male', 'HR', 'Good'),
(18, 'Penelope Reed', 58000.00, 27, 'Female', 'Finance', 'Excellent'),
(19, 'Quentin Adams', 53000.00, 25, 'Male', 'Marketing', 'Average'),
(20, 'Rachel Hill', 65000.00, 22, 'Female', 'Sales', 'Good'),
(21, 'Samuel Turner', 71000.00, 30, 'Male', 'IT', 'Excellent'),
(22, 'Tiffany Gonzalez', 59000.00, 28, 'Female', 'HR', 'Good'),
(23, 'Ulysses Rodriguez', 68000.00, 29, 'Male', 'Finance', 'Excellent'),
(24, 'Victoria Lopez', 54000.00, 27, 'Female', 'Marketing', 'Average'),
(25, 'William Foster', 69000.00, 24, 'Male', 'Sales', 'Good'),
(26, 'Xavier Parker', 61000.00, 26, 'Male', 'IT', 'Excellent'),
(27, 'Yvonne Stewart', 58000.00, 31, 'Female', 'HR', 'Good'),
(28, 'Zachary Reed', 70000.00, 33, 'Male', 'Finance', 'Excellent'),
(29, 'Abigail Cooper', 56000.00, 28, 'Female', 'Marketing', 'Average'),
(30, 'Benjamin Turner', 73000.00, 35, 'Male', 'Sales', 'Good');
INSERT INTO GM_EMLPOYEE (EID, ENAME, ESALARY, EAGE, EGENDER, EDEPT, ERATINGS)
VALUES
(31, 'John DAVID', 50000.00, 35, 'Male', 'IT', 'Excellent'),
(32, 'JOHNY Smith', 60000.00, 32, 'Female', 'HR', 'Good'),
(33, 'Bob Johnson', 75000.00, 31, 'Male', 'Finance', 'Excellent'),
(34, 'BOBY Williams', 80000.00, 33, 'Female', 'Marketing', 'Average'),
(35, 'CharMAN Brown', 55000.00, 37, 'Male', 'Sales', 'Good');
INSERT INTO GM_EMLPOYEE (EID, ENAME, ESALARY, EAGE, EGENDER, EDEPT, ERATINGS)
VALUES
(31, 'John KUMARREDDY ', 50000.00, 35, 'Male', 'IT', 'Excellent'),
(32, 'RAJ REDDY ', 60000.00, 32, 'Female', 'HR', 'Good'),
(33, 'Bob REDDY Johnson', 75000.00, 31, 'Male', 'Finance', 'Excellent'),
(34, 'BOBY Williams REDDY ', 80000.00, 33, 'Female', 'Marketing', 'Average'),
(35, 'REDDY CharMAN Brown', 55000.00, 37, 'Male', 'Sales', 'Good');
Requiremt#1: Hi Rajeskar, please provide names which start JOHN
in the EMP
SELECT * fROM GM_EMLPOYEE WHERE ENAME LIKE 'JOHN%'
SELECT * fROM GM_EMLPOYEE WHERE ENAME LIKE 'BOB%'
Requiremt#2: Hi BHARAGAV, please provide names which ENDS WITH SON
in the EMP
SELECT * fROM GM_EMLPOYEE WHERE ENAME LIKE '%SON'
-- INTERVIEW QUESTIONS --- WAQ TO RETURN ALL GMAIL.COM RECORDS
SELECT * fROM CARSALES WHERE CustomerEmail LIKE '%@GMAIL.COM';
SELECT * fROM CARSALES WHERE CustomerEmail LIKE '%@YAHOO.COM';
SELECT * fROM GM_EMLPOYEE WHERE ENAME LIKE '%REDDY%';
---BETWEEN (NUMERICS)
SELECT * fROM GM_EMLPOYEE WHERE EAGE BETWEEN 30 AND 45;
---BETWEEN (DATES)
SELECT * fROM CARSALES WHERE SaleDate BETWEEN '2020-01-01' AND '2024-12-31';
--CLUASES
WHERE
--GROUP BY
--Requriement#1: Hi Rahul Awasthi, Please provide Each Carmake total profits
SELECT carmake,sum(profit) fROM CarSales GROUP BY carmake;
SELECT carmake,sum(profit) as TotalProfits fROM CarSales GROUP BY carmake;
--Aggregation: sum,min, max, avg and count
--Alias : (nick names): Tables NAmes and coloumns name s
--Requriement#1: Hi Swathi Acharya, Please provide Each Carmake count
SELECT carmake,count(profit) fROM CarSales GROUP BY carmake;
SELECT carmake,count(profit) as TotalCount fROM CarSales GROUP BY carmake;
--group by with order by
-- to see top selling cars list
SELECT carmake,count(carmake) as TotalCount fROM CarSales GROUP BY
carmake order by totalcount desc ;
--- HAVING CLUASE: TO APPLY THE CONDTIONS ON THE GROUP BY CLUASE
-- INTERVIEW QUESTION: CAN WE USE HAVING CLUASE WITH OUT GROUP BY CLUASE?
--EX: KID(HAVING), PARENTS(GROUP BY)
--REQUIREMENT#1: Hi Anusha, Please provide the total carsmake count which are
greater than 2000
SELECT carmake,count(profit) as TotalCount fROM CarSales GROUP BY carmake
having count(profit)>2000 ;
SELECT carmake,count(profit) as TotalCount fROM CarSales GROUP BY carmake
having count(profit)>2000 order by totalcount;
SELECT carmake,count(profit) as TotalCount fROM CarSales GROUP BY carmake
having count(profit)>2000 order by totalcount desc;