Professional Documents
Culture Documents
Imp SQL Solved
Imp SQL Solved
Imp SQL Solved
Example 1:
Example-2:
Example 4:
>>select t.name||' Vs '||t1.name from mock t
inner join mock t1
on t1.id>t.id;
example 5:
# find the total distance travelled by each user (name,total_distance_travelled)
>>
select u.name,r.passenger_id,sum(distance_travlled)as total_distance_travelled from
rides r
inner join users u
on u.id=r.passenger_id
group by u.name, passenger_id
order by total_distance_travelled;
>>
select u.name,sum(distance_travlled)as total_distance_travelled from rides r
inner join users u
on u.id=r.passenger_id
group by u.name
order by total_distance_travelled;
Example 6:
# find even employee id
>> select * from employees where mod(employee_id,2)=0;
#find odd employee id
>> select * from employees where mod(employee_id,2)!=0;
Example:7
Rownum is a pseudo which generate unordered sequence number.
#fetch first 50% records from employ table
Example 8:
# Query to Find unique records
select name,sal,count(*) from emp_dup
group by name,sal
having count(*)=1;
ROWID:
- it is unique identification given to the each row in the table
- for duplicate also there will be different unique identification
- rowid is useful to delete duplicate records.
Syntax:
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
WHEN conditionN THEN resultN
ELSE default_result
END
# Order category
# replace - emp fisrt_name when Jane then Balaji, when bob then Ram and keep
remaining as its
#student grades
select stud_id,name,marks,
case when marks between 85 and 100 then 'O'
when marks between 70 and 80 then 'A+'
when marks between 60 and 70 then 'A'
when marks between 50 and 60 then 'B'
when marks between 40 and 50 then 'C'
else 'FAIL' end as grades from student;
# Find total marks for each gender
(or)
SELECT
CEIL(ball_no / 6) AS over_number,
SUM(run) AS runs_in_over
FROM Cricket
GROUP BY CEIL(ball_no / 6);
# count no of overs
SELECT round(MAX(ball_no) / 6) AS overs_count
FROM Cricket;
# find the total cash amount and online amount
Example 12:
Fetch total revenue of each product.
Example 13:
# find highest salaried employ
# Find highest salary between 3 and 5
select * from
(select employee_id,first_name,last_name,salary,
dense_rank() over(order by salary desc) as rnk
from employees
)
where rnk between 3 and 5;
# Find minimum salary along with all employee details (department wise)
select department_id,first_name,last_name,salary,
min(salary) over(partition by department_id) as min_sal_dpt
from employees;
# Find the employees who get salary more than the average salary of the all
employees.
select * from
(
select department_id,first_name,last_name,salary,
avg(salary) over() as avg_sal
from employees
)
where salary> avg_sal;
(or)
select * from employees
where salary >
(select avg(salary) from employees);
# Find the employees who get salary more than the minimum salary of the all
employees.
select * from
(
select department_id,first_name,last_name,salary,
min(salary) over() as min_sal
from employees
)
where salary> min_sal;
(or)
select * from employees
where salary >
(select min(salary) from employees);
Examples on Join:
# Fetch employees who get highest salary in each department
select * from
(select d.department_id,d.department_name,e.first_name,e.last_name,e.salary,
dense_rank() over(partition by e.department_id order by e.salary desc) as rnk
from employees e
inner join departments d
on e.department_id=d.department_id)
where rnk=1;