Oracle relational database, querying/joining multiple tables - oracle

I know most people here tend to frown on people asking homework questions on here, but I am really stumped on one question in my database systems class.
List all cities (city name and zip code where there is at least one property for rent managed by staff who works in the branch office at ‘1119 Leighton Ave’. Order your result by ascending order of city name. Otherwise, zero point.
The database schema is:
Branch (bNo, street, zipcode)
Staff (sNo, fName, lName, position, sex, dob, salary, bNo)
Property (pNo, street, zipcode, type, room, rent, oNo, sNo, bNo)
Owner (oNo, fName, fName, street, zipcode, phone)
Client (cNo, fName, lName, phone, prefType, maxRent)
Viewing (pNo, cNo, viewDate, cmmt)
Zip (city, state, zipcode)
And this is the query that I have tried to put together, very unsuccessfully...
SELECT z.city, p.zipcode
FROM Zip z, Property p
WHERE p.bNo = (SELECT bNo
FROM Branch
WHERE street = '1119 Leighton Ave');
I am having a bit of hard time with the joins. This statement above does NOT even come close to working right. I am digging through my book and the internet, trying to find some sort of help. Any help would be greatly appreciated. Thanks in advance.
UPDATE
I am currently trying:
SELECT DISTINCT Zip.city, Zip.zipcode AS Zip
FROM Zip
JOIN Property ON Zip.zipcode = Property.zipcode
JOIN Branch ON Zip.zipcode = Branch.zipcode
WHERE Property.bNo = (SELECT bNo
FROM Branch
WHERE Street = '1119 Leighton Ave')
ORDER BY Zip.city;
The output looks a lot better, but I don't think it is correct. I am checking it now.
UPDATE #2
Okay, so by running this query:
SELECT DISTINCT zipcode
FROM Property
WHERE bNo = 'B001';
and just as an FYI B001 is the branch number for the Branch located at 1119 Leighton Ave. Anyways, that command gave me the following zip codes:
36205
36251
36264
36206
36277
36272
36265
36203
36201
When I run the above command:
SELECT DISTINCT Zip.city, Zip.zipcode AS Zip
FROM Zip
JOIN Property ON Zip.zipcode = Property.zipcode
JOIN Branch ON Zip.zipcode = Branch.zipcode
WHERE Property.bNo = (SELECT bNo
FROM Branch
WHERE Street = '1119 Leighton Ave')
ORDER BY Zip.city;
This is my output:
City Zip
--------------------- -----
ANNISTON 36206
JACKSONVILLE 36265
So, I believe that something is a little off with this query. But you guys have given me a GREAT start, I will continue playing with it and I am confident that I can probably figure it out. Thanks a ton guys.
I think I got it figured out thanks to all the help from everyone.
SELECT DISTINCT Zip.city, Zip.zipcode AS Zip
FROM Zip
JOIN Property ON Zip.zipcode = Property.zipcode
JOIN Branch ON Zip.zipcode = Property.zipcode
JOIN Staff ON Staff.bNo = Branch.bNo
WHERE Property.bNo = (SELECT bNo FROM Branch WHERE Street = '1119 Leighton Ave')
ORDER BY Zip.city

There are multiple ways to write JOIN queries in Oracle. A more standard SQL way is to do something like this:
SELECT t1.colNameA, t1.colNameB, t2.colNameC
FROM t1
JOIN t1.pid = t2.fkid
WHERE
t1.colNameA='whatever'
With that in mind, you might try the following steps:
List out all the columns that you need:
SELECT Zip.city, Zip.zipcode
List out all the tables that you need, including the relationships
between them:
FROM Zip
JOIN Property ON Zip.zipcode=Property.zipcode
JOIN Branch ON Zip.zipCode=Branch.zipcode
JOIN etc
Add the WHERE criteria.
WHERE Branch.street='1119 LeightonAve'
AND etc
Add ORDER BY clause
ORDER BY Zip.city
This is not the complete answer, but hopefully helps get you in the right direction.
EDIT #2
After your edit, I would suggest you need to research the keyword "DISTINCT". Something like this might be more what you are looking for:
SELECT DISTINCT Zip.city, Zip.zipcode

Try this
SELECT z.city, p.zipcode
FROM Zip z
JOIN Property p ON z.zipcode = p.zipcode -- city name and zip code where there is at least one property for rent
JOIN Staff s ON p.sNo = p.sNo -- managed by staff
JOIN Branch b ON s.bNo = b.bNo AND b.street = '1119 Leighton Ave' -- who works in the branch office at ‘1119 Leighton Ave’
ORDER BY z.city
JOIN(INNER JOIN) makes sure both table participating has the same value in at least one row, otherwise no record is returned.

Related

LEFT JOIN slowing the query

SELECT COALESCE(clnt.ename,clientid) NAME, sum(turnover)
FROM A
LEFT JOIN (SELECT DISTINCT csid,code,CP.ename
FROM Senna
LEFT JOIN (SELECT DISTINCT csid,ename FROM C1) CP ON Senna.csid=CP.csid) clnt ON clientid = clnt.CODE
GROUP BY COALESCE(clnt.ename,clientid)
The above query is taking a lot of time to run and the reason for that is that the Senna and the CP tables are reference tables. I am trying to work a way to the improve the performance so that select clause for code field in the senna table is only picked for the clientid which are contained in the table A. The left join between Senna and the CP table is the reason for the issue of slowness but if somehow I can get the Senna table to fetch records based on only the clientid from A that will improve the performance massively.
Is there a way that the clientids can be included in the where clause of the senna table so that when the join is being done with the cp table, the final join of clientid = clnt.CODE is done on a smaller data set. Essentially, there are a number of records in the senna table which can be filtered out in order to improve performance. The ultimate goal is to pull the ename.
Bear in mind there would be 100s of clientid in table A as well.
Please any help would be appreciated.
I don't know that I understand what you need as an output. Please see whether the below Oracle query helps. If not, please explain your use-case more (say you want to list all the records from table A that matches records with table C1 etc or something similar to it for better understanding)
SELECT
COALESCE(clnt.ename, clientid) NAME,
sum(turnover)
FROM
A
LEFT JOIN (
SELECT
DISTINCT senna.csid,
senna.code,
CP.ename
FROM
Senna senna
join C1 CP on senna.csid = CP.csid
) clnt ON clientid = clnt.CODE
GROUP BY
COALESCE(clnt.ename, clientid);

JOIN 4 tables in one (Oracle R11)

I need to create a query that shows the "Legal Entity", "Application Name", "Close Date" and "Period" I'm working with Oracle R11, Right now I've found the query for
"Legal Entity"
SELECT name
FROM hr_organization_information HOI
INNER JOIN hr_all_organization_units HAOU
ON HOI.ORGANIZATION_ID = Haou.Organization_Id
WHERE HOI.org_information_context LIKE 'Legal Entity Accounting'
ORDER BY NAME ASC;
and for "Application Name, Close Date, Period"
SELECT A.APPLICATION_ID,
B.APPLICATION_NAME,
TO_CHAR(A.END_DATE,'HH24:MI DD-MON-YYYYI'),
A.PERIOD_NUM
FROM GL_PERIOD_STATUSES A
INNER JOIN FND_APPLICATION_TL B ON A.APPLICATION_ID = B.APPLICATION_ID
WHERE A.Application_Id=101
AND LANGUAGE='US'
OR A.APPLICATION_ID=200
AND LANGUAGE='US'
OR A.APPLICATION_ID=222
AND LANGUAGE='US';
Separately but I haven't found the way to join them in one query, can you help me with that?
Antonio, I think Brian has given you sound advice. Posting to an EBS forum (or whatever application this is) might also be worthwhile if his advice has not lead you to the answer. I will offer that sometimes the way to join table_A and table_B is through table_C. That is, if you do not find any directly related data in the queries of one se to one of the tables in the other set then look at the FK defined on and pointing to these tables to see if you can find a table not currently part of either query that relates the sets. You figure out how to join each of your current queries to it and that is how you join the two queries together.
Thank you all!
The advices that all of you gave to me were useful, I've found the table HR_LEGAL_ENTITIES (Table C) that have two columns that allow me to join Table A with Table B, the final query was:
SELECT HAOU.NAME,
FAT.APPLICATION_NAME,
TO_CHAR(GPS.END_DATE,'HH24:MI DD-MON-YYYY'),
GPS.PERIOD_NUM
FROM HR_ALL_ORGANIZATION_UNITS HAOU
INNER JOIN HR_LEGAL_ENTITIES HLE
ON HLE.ORGANIZATION_ID = HAOU.ORGANIZATION_ID
INNER JOIN GL_PERIOD_STATUSES GPS
ON HLE.SET_OF_BOOKS_ID = GPS.SET_OF_BOOKS_ID
INNER JOIN FND_APPLICATION_TL FAT
ON GPS.APPLICATION_ID = FAT.APPLICATION_ID
WHERE GPS.Application_Id IN (101,200,222) AND LANGUAGE='US'
ORDER BY NAME ASC;
Regards!

apex shuttle not displayed

I'm developing a functionality that fills a collection, using two select lists, with a shuttle . I'm not getting the correct way to display the name, and recover its correspondent id, on shuttle's select item at right side, when I change an specific list, after repeated interventions. I developed a procedure to update, insert or delete the selected items on shuttle, and I suppose its works well after several tests on sql commands.
My test case uses three tables : contracts, worksations and employees. I intend to insert the employees of any workstation of a given contract to another contract. That new contract, whose will receive the employees, must have its owns workstations, previously inserted. The general structure of the tables is:
contracts : pk_contract, number_contract, company_name etc...
workstations : pk_workstation, fk_contract, description etc...
employees: pk_employee, fk_workstation, employee_name, employee_sex etc...
I created an app to demonstrate it: https://apex.oracle.com/pls/apex/f?p=43921​
USER: test,
PASSWORD : TEST
Page 2- Migrate Employees - has three major regions, and 2 another to display information inserted :
Contracts with a select list, that must choice, preferrable the contract 070/2016, witch have workstation inserted, and a display field with static value, representing a contract, with workstations and employees attached.
workstations: with two select list, the gordian knot, I suppose : old workstations, related original contract's wokstations - related to display field, at contract's region; and new workstation, related to new contract selected above.
Employees: with a shuttle that represents, at left side, a bunch of employees, of a given original workstation and button witch do nothing.
Two more regions representing a classical report, only to inform the inserted, updated or deleted employees, from shuttles right size, using a collection. I made two report's regions because don't know make joins with collections and tables. This collection have the employee id, old_workstation id, new_workstation id, and new contract id, for further migration, not yet implemented.
I'm submiting almost all the fields, for recover its values on apex session's state.
Apparently, the collection works well, with a procedure, but when I choose again the original worksation the shuttle don't display, on right size, the previous inserted employee on that workstation. I configured on my list of values to not include extra values, when i change it, it shows the employee's ids, not their names, regards to source type listed here below:
SELECT e.pk_employee
FROM tb_employee e
INNER JOIN tb_workstation pt
ON pt.pk_workstation = e.fk_workstation
WHERE pt.typo IS NOT NULL
AND e.fk_workstation = p2_original_workstation
AND e.pk_employee IN (
SELECT to_number(c001) AS id_employee
FROM apex_collections
WHERE collection_name = 'WORKSTATION_EMPLOYEES');
My shutlle's lov has this rationale:
SELECT e.name, e.pk_employee
FROM tb_employee e
INNER JOIN tb_workstation pt
ON pt.pk_workstation = e.fk_workstation
WHERE pt.typo IS NOT NULL
AND e.fk_workstation = p2_original_workstation
AND e.pk_employee NOT IN (
SELECT to_number(c001) AS id_employee
FROM apex_collections
WHERE collection_name = 'WORKSTATION_EMPLOYEES');
Could anyone help me on this issue?
Regards!
I consider this a regular and fine solution. The shuttle permits recover entire registers on left side, changing it to another original workstation. After have removed the last "and" subclause, added at SHUTTLE an source sql query - return colon separated value, like this:
select e.pk_employee from employee e
inner join workstations w on w.pk_workstation = e.fk_workstation
where w.typo is not null and e.fk_workstation = :P2_ORIGINAL_WORKSTATION and e.pk_employee in (
SELECT TO_NUMBER(c001) as id_employee FROM APEX_collections WHERE collection_name = 'WORKSTATION_EMPLOYEES' and c002 = :P2_NEW_WORKSTATION);
But for purpose of goood usabilty, and my better interpretation that what shuttle obect does, I reinsert the 'and' clause with an 'and' more, reflecting that on lov has all employees of a given original workstation, except by the employees also inserted and that ones became from another new workstations. This particularité avoids the user thinks that a employee previously attached, on new workstation needed to be inserted:
select e.name, e.employee from employees e
inner join workstations w on w.pk_workstation = e.workstation
where w.typo is not null and e.worktation = :P2_ORIGINAL_WORKSTATION and e.pk_employee not in (
SELECT TO_NUMBER(c001) as id_employee FROM APEX_collections WHERE collection_name = 'WORKSTATION_EMPLOYEES' and c002 != :P2_NEW_WORKSTATION)
I could be consider use without these cited subclauses, using a disable jquery, in case of same original_wrkstion but at a diferent new, at left size, however its will be an improvement, and I need to understand the jqueries features. I consider this an basic and good, yet functional solution.

Which hotel has the most stayings?

I am pretty new at Oracle, and I am doing some examples which I found on one of the websites to learn something new. One of the tasks is to get from tables, which hotel has the most stayings. I find this one problem realy complex and I can not solve it by myself, so I Will be happy if someone could help me with soultion and explanation why is is that way.
I understood that question in that way, that you have to connect VISITOR, VISITED, HOTEL, OWNING and OWNER tables.
To explain. OWNING table is connecting OWNERs with their HOTELs and VISITED table is connecting VISITORs with HOTEL in which they stayed.
This are my tables for that example: Pastebin link to my example.
What I have tried so far is:
SELECT visitor.id, visitor.name, visitor.surname, hotel.name
FROM visitor
LEFT JOIN visiting
ON visitor.id = visiting.tk_visitor
LEFT JOIN hotel
ON hotel.id = visiting.tk_hotel
ORDER BY hotel.id ASC;
This is connecting three tables, so I get Visitors and Hotels connected in one table, but i can't go further.
I hope that Will be able to help me, so I can learn something new on that example.
Thanks in addition!
which hotel has the most stayings[?]
Each visit corresponds to one row in table visited, therefore you want to count rows of visited on a per-hotel basis. That's this:
select tk_hotel, count(*) as visits
FROM visited
GROUP BY tk_hotel
If you want only the one (one of the ones) with the most visits then you can order the results by visit count, and take only the first. To get the name of the hotel instead of just its id, you should join the previous result to the hotel table:
SELECT h.name, mv.visits
FROM (
SELECT tk_hotel, count(*) as visits
FROM visited
GROUP BY tk_hotel
ORDER BY count(*) DESC
) mv
JOIN hotel h ON h.id = mv.tk_hotel
WHERE ROWNUM = 1
;
Update
If instead you want all the rows having the maximum number of visits, then that's a different kettle of fish. Here's a way to express it:
WITH visit_count AS (
SELECT tk_hotel, count(*) as visits
FROM visited
GROUP BY tk_hotel
)
SELECT h.name, vc.visits
FROM
hotel h
JOIN visit_count vc ON h.id = vc.tk_hotel
WHERE vc.visits = (SELECT MAX(visits) FROM visit_count)

Comparing values from two different tables in SQL Plus Oracle

I have multiple tables with some foreign keys in some. Here are the tables;
Doctor
Doctor_id, FirstName, SecondName,etc...
Hospital
Hospital_id, Name...
Job
Job_id
fk Doctor_id
fk Hospital_id
I'm trying to show a list of doctors that works in 'X' hospital. How would I run this query?
SELECT FirstName, SecondName
FROM Doctor, Job, Hospital
WHERE Hospital.Name = 'HospitalName' AND Job.hospital_id = Hospital.hospital_id;
I'm not sure if that particular query is right because it shows every single doctor (not the ones that work in 'HospitalName'. If that is correct than I guess the foreign keys ain't right?
Thanks in advance. DG
You should learn to use proper join syntax. Then mistakes like this are much less likely to occur:
SELECT d.FirstName, d.SecondName
FROM Doctor d join
Job j
on d.Doctor_id = j.Doctor_Id join
Hospital h
on j.hospital_id = h.hospital_id
WHERE h.Name = 'HospitalName';
This also adds in table aliases for every column, so someone reading the query knows where they are coming from.
You are missing one join condition.
SELECT FirstName, SecondName
FROM Doctor, Job, Hospital
WHERE Hospital.Name = 'HospitalName'
AND Job.hospital_id = Hospital.hospital_id
AND job.Doctor_id = Doctor.doctor_id;

Resources