SQLL
SQLL
-- Use the editor to create new tables, insert data and all other SQL operations.
-- Q3: SQL QUERY TO FETCH UNIQUE VALUES OF DEPT FROM WORKER TABLE
SELECT DISTINCT dept FROM WORKER;
-- 6: QUERT TO PRINT THE FIRST_NAME FROM WORKER AFTER REMOVING SPACES FROM
RIGHTSIDE
SELECT RTRIM(FIRST_NAME) FROM WORKER;
-- 7: SQL QUERY THAT FETCHES THE UNIQUE VALUE OF DEPT FROM WORKER AND PRINTS ITS
LENGTH
SELECT DISTINCT(department), LENGTH(department) FROM WORKER
-- 9 SQL query to print the FIRST_NAME and LAST_NAME from Worker table into a
single column COMPLETE_NAME
SELECT CONCAT(FIRST_NAME," ",LAST_NAME) AS COMPLETE_NAME FROM WORKERS
-- 10 query to print all Worker details from the Worker table order by FIRST NAME
Ascending
SELECT * FROM WORKER ORDER BY FIRST_NAME ASC;
-- 11 query to print all Worker details from the Worker table order by FIRST NAME
Ascending AND DEPT DESCENDING
SELECT * FROM WORKER ORDER BY FIRST_NAME ASC, department DESC;
-- 12 SQL QUERY TO FETCH DETAILS FOR WORKER WITH FIRST_NAME AS VIPUL AND SATISH
FROM WORKER
SELECT * FROM WORKER WHERE FIRST_NAME IN("VIPUL","SATISH")
-- 13 query to print details of workers excluding first names, "Vipul" and "Satish"
from Worker table.
SELECT * FROM WORKER WHERE FIRST_NAME NOT IN("VIPUL","SATISH")
-- 16 query to print details of the Workers whose FIRST_NAME ENDS WITH 'a'
SELECT * FROM WORKER WHERE FIRST_NAME LIKE'%a'
-- 17 Write an SQL query to print details of the Workers whose FIRST_NAME ends with
'h' and contains six alphabets
SELECT * FROM WORKER WHERE FIRST_NAME LIKE'_____h'
-- 18 Write an SOL query to print details of the Workers whose SALARY lies between
100000 and 500000.
SELECT* FROM WORKER WHERE SALARY BETWEEN 10000000 AND 25000000
-- 19 query to print details of the Workers who have joined in Feb 2014
SELECT * FROM WORKER WHERE YEAR(JOINING_DATE)=2014 AND MONTH(JOINING_MONTH)=02;
-- BHAARI ON MIND
-- 21 QUERY TO FETCH NUMBER OF WORKER FOR EACH DEPT IN DESCENDING ORDER
SELECT DEPARTMENT, COUNT(EMPLOYEE) FROM WORKER GROUP BY DEPARTMENT ORDER BY
COUNT(EMPLOYEE) DESC
WITH rankedsal AS (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS ranked_sal
FROM employee
)
SELECT salary
FROM rankedsal
WHERE ranked_sal = 3;
--TO GET ALL THE DATA OF PEOPLE WITH 3RD HIGHEST SALARY
with rankedsal as(
select *, dense_rank() over(order by empsal desc) as sal_rank
from employee
)
SELECT *
FROM rankedsal where sal_rank = 3;
select * from employee where empid = (select min(empid) from employee); --prints
the first data of employee table
select * from employee where empid = (select max(empid) from employee); --prints
last data od employee table
--between
select * from employee where empsal between 100000 ans 300000
--Q5(a): Write a query to find all the Employee names whose name:
--• Begin with 'A'
--• Contains 'A' alphabet at second place
--• Contains 'Y' alphabet at second last place
--• Ends with 'L' and contains 4 alphabets
--• Begins with 'V' and ends with 'A'
SELECT * FROM Employee WHERE EmpName LIKE 'A%';
SELECT * FROM Employee WHERE EmpName LIKE '_a%';
SELECT * FROM Employee WHERE EmpName LIKE '%y-';
SELECT * FROM Employee WHERE EmpName LIKE '----l';
SELECT * FROM Employee WHERE EmpName LIKE 'V%a';
--2: DELETE
DELETE from employee where empid in (select empid from employee group by empid
having count(*)>1);
--bonus question
select empid,empname,
sum(case when city = 'mathura' then salary end) as mathura,
sum(case when city = 'pune' then salary end) as pune,
sum(case when city = 'delhi' then salary end) as delhi;
from employee group by empname, empid;
--FIND THE DIST TRAVELLED BY CAR: TECH TFQ :TABLE IS GIVEN WITH CUMULATIVE DISTANCE
COVERED ND WE NEED TO FIND THE ACTUAL DIST TRAVELLED EACH DAY
SELECT *,
cumulative_dist - LAG(cumulative_dist, 1, 0) over (partition by cars order by days)
as dist_travelled_per_Day
from cars;
--given a table of states and dist b/w them remove the redundant data
with cte as(
select city1, city2, row_number() over (order by dist)--this over() can also
remain empty
as src_dest_dis from dist_table
)