How does select query works? - sql

I having trouble with this query
it is executing quit well but I cannot make out
how is this select statement working.
Any help or explanation on this problem will be appreciated ..
thank you
these are my tables and query
here am looking for the employee who lives in same city as the the company for which they work
Table:-emp
eid name street city
----------- ---------------- ------------- ------------
1 yeman asd vasai
2 aksh adssd mumbai
3 chintan ghfgh mumbai
4 samual ghfdgh bandra
5 ddlj fghfgh andheri
6 jack fghnfg Bandra
7 bridge gfhfgh vasai
8 rahim ghfgh mumbai
9 chirag fghfghfg bandra
10 mistry hhhty bandra
11 ravi tytey andheri
Table:- company
cid companyname city
----------- ------------------- ------------
1 Vasai Industries vasai
2 Mumbai Pharmacy mumbai
3 bandra loft bandra
4 andheri tactics andheri
Table:= works
eid cid salary
----------- ----------- -----------
1 1 200
2 3 4831
3 4 4457
4 2 20001
5 1 32221
6 2 224
7 3 784
8 1 336
9 3 2489
10 2 4789
11 1 22541
Query
select * from emp
where eid
IN (select eid from works
where cid=(select cid from company
where city=emp.city))

why not use this query with joins and its easy to understand then a bunch of subqueries.
select * from emp
inner join works on works.eid = emp.eid
inner join company on company.city=emp.city

Explanation:
1.select cid from company where city=emp.city
Here you are getting city id regarding cities which are same in emp and company
2.
select eid from works
where cid=(select cid from company
where city=emp.city)
Here you getting collection of id's from works table which cid is same in emp and company
3.
select * from emp
where eid
IN (select eid from works
where cid=(select cid from company
where city=emp.city))
here you are getting all records based on emp id's whose cities are same in emp and city

Related

Select query to return no records

I have Table 1 : EMP as below
ID NAME CITY AMT
-------------------------------------------
1 sajani Bangalore 20
2 Prashanth Bangalore 10
3 Jayvin Bangalore 10
Table 2: EMP1
ID NAME1 CITY1 AMT1
---------------------------------------------
1 Sajani Bangalore 10
1 Sajani Bangalore 10
2 Prashanth Bangalore 10
3 Jayvin Bangalore 10
ID is the Key and is common in both the files. I want a Select SQL statement which states table 1 = table 2 and once select query is executed, it should return 0 records.
Use MINUS operator
SELECT ID, NAME, CITY, AMT
FROM EMP
MINUS
SELECT ID, NAME1, CITY1, SUM(AMT1)
FROM EMP1
GROUP BY ID, NAME1, CITY1
Thanks, I figured out the solution
Select * from emp
where amt <>(
select sum(e1.amt1)
from emp1 e1
where e1.id =emp.id
)

delete multiple rows from different tables on oracle

I have a two table.One of them is student, the other one is salary.
Student table
id | name | code | status
1 | steven | 123 | 100
2 | joe | 678 | 200
3 | paul | 758 | 100
Salary table
id | code | status | currency
1 | 123 | 100 | euro
2 | 678 | 200 | dolar
3 | 758 | 520 | yuan
I want to delete row1 from Student table and row 1 and 2 from Salary table because code and status fields
are same.
I write that query
delete a,b Student as a , join Salary as b
on a.code= b.code and a.status = b.status
but it is not working.I want to delete rows with one query.Do you have any idea?
Would something like this do? PL/SQL, though, not SQL.
Initial data sets:
SQL> select * from student;
ID NAME CODE STATUS
---------- ------ ---------- ----------
1 steven 123 100
2 joe 678 200
3 paul 758 100
SQL> select * from salary;
ID CODE STATUS CURREN
---------- ---------- ---------- ------
1 123 100 euro
2 678 200 dollar
3 758 520 yuan
Remove common (CODE, STATUS) combinations:
SQL> begin
2 for cur_r in (select code, status from student
3 intersect
4 select code, status from salary
5 )
6 loop
7 delete from student where code = cur_r.code and status = cur_r.status;
8 delete from salary where code = cur_r.code and status = cur_r.status;
9 end loop;
10 end;
11 /
PL/SQL procedure successfully completed.
Result:
SQL> select * from student;
ID NAME CODE STATUS
---------- ------ ---------- ----------
3 paul 758 100
SQL> select * from salary;
ID CODE STATUS CURREN
---------- ---------- ---------- ------
3 758 520 yuan
SQL>
You can use two statements to do so easily:
delete student s where exists(
select * from student stu inner join salary sal on stu.code=sal.code and stu.status=sal.status and stu.id=s.id);
delete salary sal where not exists (select code from student stu where stu.code=sal.code);
First one to delete all the students having same code and status in both tables and second is to delete all the rows from salary table where code doesn't exist in student table.

How do I display rows from a max count?

I want to return all the data, from max count query with hospital that has most number of patients. What I seem to be getting when I try to nest queries is display of all rows of hospital data. I've tried to look at similar questions in stack overflow and other sites it seems to be a simple query to do but i am not getting it.
select max(highest_hospital) as max_hospital
from (select count(hospital) as highest_hospital
from doctor
group by hospital)
highest_hospital
-------------
3
Doc ID Doctor Patient Hospital Medicine Cost
------ ------- ------ --------- ------ --------
1 Jim Bob Patient1 Town 1 Medicine 1 4000
2 Janice Smith Patient2 Town 2 Medicine 3 3000
3 Harold Brown Patient3 Town 2 Medicine 5 2000
4 Larry Owens Patient4 Town 2 Medicine 6 3000
5 Sally Brown Patient5 Town 3 Medicine 7 4000
6 Bob Jim Patient6 Town 4 Medicine 8 6000
Outcome should be return of 3 rows
Doc ID Doctor Patient Hospital Medicine Cost
------ ------- ------ --------- ------ --------
2 Janice Smith Patient2 Town 2 Medicine 3 3000
3 Harold Brown Patient3 Town 2 Medicine 5 2000
4 Larry Owens Patient4 Town 2 Medicine 6 3000
You can use window functions:
select d.*
from (select d.*, max(hospital_count) over () as max_hospital_count
from (select d.*, count(*) over (partition by hospital) as hospital_count
from doctor d
) d
) d
where hospital_count = max_hospital_count;
Edit:
Using GROUP BY is a pain. If you are only looking for a single hospital (even when there are ties), then in Oracle 12C you can do:
select d.*
from doctor d
where d.hospital = (select d2.hospital
from doctor d2
group by d2.hospital
order by count(*) desc
fetch first 1 row only
);
You can do this in earlier versions of Oracle using an additional subquery.

Write a query to display the names of the department and the staff count in each department, if staff not exist display the count as 0,

The code is running properly giving necessary output but one of the test cases fails.
select department_name,count(*) as staff_count
from department d,staff s
where d.department_id=s.staff_id
group by department_name
order by department_name;
Proposed grade: 50 / 100
Result Description
Failed Test
Test Case 2
Summary of tests
+------------------------------+
| 2 tests run / 1 test passed |
+------------------------------+
Sample output with necessary input data:
DEPARTMENT_NAME STAFF_COUNT
------------------------------ -----------
CSE 2
ECE 1
EEE 2
IT 2
SE 4
DEPARTMENT_ID DEPARTMENT_NAME DEPARTMENT_BLOCK_NUMBER
------------- ------------------------------ -----------------------
1 CSE 3
2 IT 3
3 SE 3
4 ECE 2
5 EEE 2
STAFF_ID STAFF_NAME DEPARTMENT_ID
---------- ------------------------------ -------------
1 Lakshmi 1
2 Venky 1
3 Senthil 2
4 Sandhya 2
5 Geetha 3
6 Tom 3
7 Rekha 3
8 Viji 3
9 Laya 4
10 Nisha 5
11 Venki 5
You need an outer join for this, not an inner join (which your old and outdated implicit join in the WHERE clause does). You also need to do the join between the two department_id columns.
select department_name, count(s.staff_id) as staff_count
from department d
left join staff s on d.department_id = s.department_id
group by department_name
order by department_name;
The left join (=outer join) will return NUL for s.staff_id for departments that do not have staff. Aggregate functions simply ignore NULL values and that's how you get the 0 for the staff count.
However your sample data does not have any department without staff.
Online example with an additional empty department: https://rextester.com/EEXE2322

SQL SELECT query to pull data from tables to calculate a sum

I have four tables in a database, what I want to do is essentially list the employee number, name department number of any projects they are working on and the total hours that they spend working on projects.
I'm trying to do this all as one single SELECT statement, but am having trouble getting it to work with Oracle SQL. My latest attempt has been
SELECT EMPLOYEE.E#, EMPLOYEE.NAME, SUM(WORKSON.HOURS)
FROM EMPLOYEE JOIN WORKSON ON EMPLOYEE.E#=WORKSON.E#
GROUP BY EMPLOYEE.E#;
Which isn't even the complete statement, and gives me the error:
ERROR at line 1:
ORA-00979: not a GROUP BY expression
What can I do to complete the statement to also include the department number and what should I be doing? Should I be using a join for this? or should I be incorporating another SELECT statement within a WHERE clause?
The tables are as follows:
EMPLOYEE:
SQL> SELECT * FROM EMPLOYEE;
E# NAME DOB S SALARY SUPER D#
----- ------------------ ----------- -- ------ -------- ---
00100 Albert 13-OCT-65 M 186.5
00110 Alvin 13-OCT-77 M 156.4 00100 1
00120 Alice 17-JUN-73 F 156.5 00100 2
00150 Bob 02-JUL-60 M 166.4 00100 3
00200 Carl 02-FEB-67 M 156.3 00100 4
00250 Douglass 14-APR-83 M 156.4 00100 5
00101 Peter 13-NOV-76 M 85.2 00110 1
00103 Ami 12-SEP-85 F 78.2 00110 1
00107 Wendy 12-SEP-88 F 68.2 00110 1
00109 Michael 12-SEP-90 M 58.2 00110 1
00125 Angela 20-NOV-90 F 56.4 00120 2
00105 Robert 15-JAN-86 M 66.2 00150 3
00136 Aban 15-JAN-90 M 55.3 00200 4
00187 Eadger 07-APR-86 M 76.5 00250 5
14 rows selected.
WORKSON:
SQL> SELECT * FROM WORKSON;
E# P# HOURS
----- ------- -----
00110 1001 10
00101 1001 20
00150 1002 10
00105 1002 10
00105 1003 20
00105 1004 20
00250 1004 15
00187 1004 25
00105 1005 15
DEPARTMENT:
SQL> SELECT * FROM DEPARTMENT;
D# DNAME MANAG MSDATE
-- ------------------ ------ ------------
1 SALES 00110 02-JAN-12
2 ACCOUNTING 00120 30-OCT-10
3 GAMES 00150 01-MAR-08
4 HUMAN RESOURCES 00200 02-JAN-13
5 SPORTS 00250 10-MAY-10
PROJECT:
SQL> SELECT * FROM PROJECT;
P# PTITLE SPONSOR D# BUDGET
------ --- -------------- ---------------------- --- -------
1001 Computation Microsoft 1 25000
1002 Study methods Education committee 3 15000
1003 Racing car Cloud Pty Ltd 3 225000
1004 Football Football club 5 35000
1005 Swimming Education committee 5 125000
Any help would be greatly appreciated. Thanks in advance!
You first find the sum of hours of each employee and then join the same with your main table. Inline view can be used for the same.
SELECT EMPLOYEE.E#, EMPLOYEE.NAME, NVL(WORKSON_AGGR.TOTAL_HOURS,0)
FROM EMPLOYEE
LEFT JOIN (SELECT E#, SUM(HOURS) as TOTAL_HOURS
FROM WORKSON
GROUP BY E# ) WORKSON_AGGR
ON (WORKSON_AGGR.E# = EMPLOYEE.E#)
Now, you can join your department table too easily.
SELECT
E.E#,
E.NAME,
NVL(WORKSON_AGGR.TOTAL_HOURS,0),
D.DNAME
FROM EMPLOYEE E
LEFT JOIN (SELECT E#, SUM(HOURS) as TOTAL_HOURS
FROM WORKSON
GROUP BY E# ) WORKSON_AGGR
ON (WORKSON_AGGR.E# = E.E#)
JOIN DEPARTMENT D ON (D.D# = E.D#)