0% found this document useful (0 votes)
17 views

Leet

The document contains several SQL queries that: 1) Count the number of customer visits without transactions and group by customer ID. 2) Find weather records where the temperature rose the next day. 3) Calculate the average processing time per machine by comparing start and end timestamps. 4) Find employees with bonuses less than $1000 or a null bonus value.

Uploaded by

Israa
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Leet

The document contains several SQL queries that: 1) Count the number of customer visits without transactions and group by customer ID. 2) Find weather records where the temperature rose the next day. 3) Calculate the average processing time per machine by comparing start and end timestamps. 4) Find employees with bonuses less than $1000 or a null bonus value.

Uploaded by

Israa
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

- select customer_id, count(visit_id) as count_no_trans from Visits

where visit_id not in( select visit_id from Transactions )


group by customer_id;

-*** rising temperature


SELECT w1.id AS Id
FROM Weather w1
JOIN Weather w2 ON w1.recordDate = DATE_ADD(w2.recordDate, INTERVAL 1 DAY)
WHERE w1.temperature > w2.temperature;
- ******** average time of process per machine
select s.machine_id ,round(sum( e.timestamp - s.timestamp ) /
count( e.process_id),3) as processing_time
from Activity e , Activity s
where e.machine_id= s.machine_id and e.process_id = s.process_id
and e.activity_type='end' and s.activity_type='start'
GROUP BY s.machine_id;

-***employee bonus
select name , bonus
from Employee e left outer join Bonus b
on e.empId = b.empId
where b.bonus < 1000 or b.bonus is null;
-***students and examinations my sql
select s.student_id, s.student_name, su.subject_name
,COALESCE(COUNT(e.subject_name), 0) attended_exams
from
Subjects su
CROSS JOIN
Students s
LEFT JOIN
Examinations e ON su.subject_name = e.subject_name
AND s.student_id = e.student_id
group by s.student_id ,su.subject_name
order by s.student_id ,su.subject_name;
-***students and examinations oracle
select s.student_id, s.student_name, su.subject_name
,NVL(COUNT(e.subject_name), 0) attended_exams
from
Subjects su
CROSS JOIN
Students s
LEFT JOIN
Examinations e ON su.subject_name = e.subject_name
AND s.student_id = e.student_id
group by s.student_id ,s.student_name,su.subject_name
order by s.student_id ,su.subject_name;
-******managers with at least five direct reports
SELECT name
FROM Employee
WHERE id in (
SELECT managerId
FROM Employee
GROUP BY managerId
HAVING COUNT(managerId) >= 5
)
-******* project employees average experience
select p.project_id , round(sum(e.experience_years) / count (p.employee_id) ,2)
average_years
from Project p,Employee e
where p.employee_id= e.employee_id and e.experience_years is not null
group by p.project_id;
-****** percentage of users attended a contest
select contest_id , round(
count (r.user_id)/(select count(user_id) from Users) * 100 ,2) percentage
from Register r
group by contest_id
order by percentage desc, contest_id asc
-******** user activity for tha past 30 days
select activity_date day, count(distinct user_id) active_users
from Activity
where activity_date > '2019-06-27' and activity_date < '2019-07-27'
group by activity_date
-**** the numbers of employees which reports to each employee
select a.employee_id , a.name , count(b.reports_to) reports_count,
round(sum(b.age)/count(b.reports_to),0) average_age
from Employees a, Employees b
where a.employee_id = b.reports_to
group by a.employee_id , a.name
order by a.employee_id
-*************
MAX(a.primary_flag = 'Y') = 1: Indicates that the employee has
at least one department marked as the primary department.
The MAX(a.primary_flag = 'Y') condition checks if there is at least one 'Y' value
for
the primary_flag, meaning the employee has a primary department.
SELECT a.employee_id, a.department_id
from Employee a ,Employee b
where a.employee_id = b.employee_id
GROUP BY a.employee_id, a.department_id
HAVING COUNT(a.department_id) = 1 OR max(a.primary_flag = 'Y') = 1;

You might also like