Oracle SQL Question - Need Help - sql

So I've been doing this all night - can't quite understand my homework, and sadly my professor is unavailable on the weekend. I've posted a few of these questions, this being the last one. I've got something to go on, but it needs working (and coming out of this I'd love to fully understand the answer so I don't need help on something similar again). Here it goes: Find the name and the phone number of the theaters that show the maximum number of movies. Make sure your query works when there is a tie between several theatres.
Here are my table declares (and thank you to EVERYONE helping me out tonight, I owe you big time).
CREATE TABLE Theatres (
Name varchar2(50) not null,
City varchar2(50) not null,
State varchar2(50) not null,
Zip number not null,
Phone varchar2(50) not null,
PRIMARY KEY (Name)
);
CREATE TABLE Movies (
Title varchar2(100) not null,
Rating NUMBER not null,
Length NUMBER not null,
ReleaseDate date not null,
PRIMARY KEY (Title),
CHECK (Rating BETWEEN 0 AND 10),
CHECK (Length > 0),
CHECK (ReleaseDate > to_date('1/January/1900', 'DD/MONTH/YYYY'))
);
CREATE TABLE ShownAt (
TheatreName varchar2(50) not null,
MovieTitle varchar2(100) not null,
PRIMARY KEY (TheatreName, MovieTitle),
FOREIGN KEY (TheatreName) REFERENCES Theatres(Name),
FOREIGN KEY (MovieTitle) REFERENCES Movies(Title)
);
I'm trying to apply some of the things I've learned from StackOverflow members help in other questions, but I'm not sure how to return something based on the max results of a column. Any help would be greatly appreciated.

Here's one way.
With T As
(
SELECT T.Name, T.Phone,
RANK() OVER (ORDER BY COUNT(S.MovieTitle ) DESC) AS Rnk
FROM Theatres T
JOIN ShownAt S ON S.TheatreName= T.Name
GROUP BY T.Name, T.Phone
)
SELECT Name, Phone
FROM T
WHERE Rnk=1;

Related

In a SELECT command, how do I use data from one table to specify data in another?

I have 2 tables. What is important is the PlayerId and the Username.
CREATE TABLE [dbo].[Run]
(
[RunId] INT NOT NULL,
[PlayerId] INT NOT NULL,
[Duration] TIME(7) NOT NULL,
[DateUploaded] NCHAR(10) NOT NULL,
[VersionId] INT NOT NULL,
PRIMARY KEY CLUSTERED ([RunId] ASC),
CONSTRAINT [FK_Run_Player]
FOREIGN KEY ([PlayerId]) REFERENCES [dbo].[Player] ([PlayerId]),
CONSTRAINT [FK_Run_Version]
FOREIGN KEY ([VersionId]) REFERENCES [dbo].[Version] ([VersionId])
);
CREATE TABLE [dbo].[Player]
(
[PlayerId] INT NOT NULL,
[Username] NCHAR(20) NOT NULL,
[ProfilePicture] IMAGE NULL,
[Country] NCHAR(20) NOT NULL,
[LeagueId] INT NULL,
[DateJoined] DATE NULL,
PRIMARY KEY CLUSTERED ([PlayerId] ASC),
CONSTRAINT [FK_Player_League]
FOREIGN KEY ([LeagueId]) REFERENCES [dbo].[League] ([LeagueId])
);
I have a select command:
SELECT
PlayerId, Duration, VersionId, DateUploaded
FROM
[Run]
(with apologies in advance for my messy made up pseudocode), what I need it to do is:
SELECT (Player.PlayerId.Username)
What I basically need it to do, is instead of giving me just PlayerId, I need it to get the corresponding Username (from the other table) that matches each PlayerId (PlayerId is a foreign key)
So say for example instead of returning
1, 2, 3, 4, 5
it should return
John12, Abby2003, amy_932, asha7494, luke_ww
assuming, for example, Abby2003's PlayerId was 2.
I've done trial and error and either nobody's tried this before or I'm searching the wrong keywords. This is using VS 2022, ASP.NET Web Forms, and Visual Basic, but that shouldn't affect anything I don't think. Any syntax ideas or help would be greatly appreciated.
try this for join the 2 Table togother
SELECT R.RunId
,R.PlayerId
,R.Duration
,R.DateUploaded
,R.VersionId
,P.Username
,P.ProfilePicture
,P.Country
,P.LeagueId
,P.DateJoined
FROM Run R
inner join Player P on R.PlayerId = P.PlayerId
Usually in this case joins are used. You can join the two tables together, give them aliases (or don't, personal preference really), then select what you need. In this case, you would probably want an inner join. Your query would probably look something like this:
SELECT p.Username FROM [Run] r
INNER JOIN [Player] p ON r.PlayerId = p.PlayerId
Then if you need to you can put a WHERE clause after that.
More about joins here

Time period SQL view

I am trying to create a SQL view on two tables I'm working with:
CREATE TABLE availability
(
doctor varchar(20) NOT NULL,
avl_date date NOT NULL,
avl_start time NOT NULL,
avl_end time NOT NULL,
CONSTRAINT pk_availability PRIMARY KEY (doctor, avl_date)
);
And
CREATE TABLE appointments
(
patient varchar(20) NOT NULL,
doctor varchar(20) NOT NULL,
apt_date date NOT NULL,
apt_start time NOT NULL,
apt_end time NOT NULL,
CONSTRAINT pk_appointments PRIMARY KEY (patient, apt_date)
);
The view I am trying to create lists all maximal time periods (apt date, apt start, apt end) during which no further appointments are possible (consider doctors’ availability as well).
Any help is greatly appreciated, thanks.
this should work if the appointment time-duration is constant. You can optimize it by re-writing with inner join instead of minus. Also, remember to put availability table above minus.
select doctor
, avl_date
, avl_start
, avl_end
from availability
minus
select doctor
, apt_date
, apt_start
, apt_end
from appointments

I am trying to VIEW 4 columns from 2 different tables I have already created in Oracle Live SQL

I want to use the VIEW command to display these 4 columns in one single schema. I have tried making a single VIEW with the first 3 columns, because they are from the the same table. Adding the one other column is where I'm struggling. I have tried the ALTER function but a VIEW schema doesn't seem to have the same edit privileges as a table would. I hope that makes sense.
create table PATIENTINFO (
PatientID number not null,
FirstName varchar2(50) not null,
LastName varchar2(50) not null,
Address varchar2(50),
City varchar2(50),
State varchar2(50),
ZipCode number(5),
Phone number(10) not null ,
Email varchar2(50),
MemberID number not null,
constraint pk_departments primary key (PatientID)
)
create table LABORDER (
LabOrderNumber number not null,
OrDate date not null,
ReqBloodTest varchar2(15) not null,
Reason varchar(50),
PatientID number not null,
constraint pk_laborder primary key (LabOrderNumber),
constraint fk_laborder_patientid foreign key (PatientID)
references PATIENTINFO (PatientID)
)
CREATE VIEW PatientBlood AS
SELECT FirstName, LastName, PatientID
FROM PATIENTINFO
Write the query you want and then create a view out of it. I started by writing the query below and then prefixed it with CREATE OR REPLACE VIEW. The example below has some randomly selected columns, change it to whatever columns you need. I chose to name my columns in the view definition, you can omit that but also do a million other things as stated in the docs
Side note: don't use mixed case for identifiers like column names/table names. It is confusing. In your case it didn't matter since you didn't use quotes, so they're case insensitive and the view below will work even though the identifiers are all lower case.
CREATE OR REPLACE VIEW laborder_v (
labordernumber,
patientid,
lastname
) AS
SELECT o.labordernumber,
p.patientid,
p.lastname
FROM laborder o
JOIN patientinfo p ON o.patientid = p.patientid;

SQL Join - Query error

So I would like to display the most experience player, at first I was going to display all players and experiencepoints then try and only display the max. so heres the code for both tables and the query itself
CREATE TABLE Player
(
Player_ID INTEGER CONSTRAINT pk_player PRIMARY KEY,
Pname VARCHAR(60),
DOB VARCHAR(10),
Heightcm NUMBER(10),
Weightkg NUMBER(10),
Position VARCHAR(20),
Team_ID INTEGER CONSTRAINT fk_player REFERENCES Team(Team_ID)
)
CREATE TABLE PlayerCareer_Performance
(
Player_Performance_ID INTEGER CONSTRAINT pk_PP PRIMARY KEY,
Player_ID INTEGER,
total_points NUMBER(10),
total_fouls NUMBER(10),
ExperiencePoints NUMBER(10),
CONSTRAINT fk_Pp FOREIGN KEY (Player_ID) REFERENCES Player(Player_ID)
)
Thats the table code, below is the query:
SELECT PLAYER.PLAYER_ID,PLAYER.PNAME,PLAYERCAREER_PERFORMANCE.EXPERIENCEPOINTS
FROM PLAYER
JOIN PLAYERCAREER_PERFOMANCE
ON PLAYERPERFROMANCE.PLAYER_ID = PLAYER.PLAYER_ID;
Player is underlined on yellow and it says "Is disconnected from the rest of join graph"
Any ideas, anyone? I cant see a fix for it :(
At first sight, you are joining the table PLAYERCAREER_PERFORMANCE but joining on a field of table called PLAYERPERFROMANCE (sic). Is that a typo?
This:
SELECT Player.Player_ID, Player.Pname, PlayerCareer_Performance.ExperiencePoints
FROM Player
JOIN PlayerCareer_Performance
ON PlayerCareer_Performance.Player_ID = Player.Player_ID
Seems to work fine (just corrected the typo): check it at sqlfiddle.
As a general suggestion, keep your code consistent... you are using different column formats (e.g.: ExperiencePoints vs total_points), and are putting all SQL query code in caps, which just makes it difficult to read and find typos
Use an inner join (updated to reflect popular demand):
select p.player_id, p.pname, pcp.experiencepoiuts
from Player p
inner join PlayerCareer_Performance pcp on pcp.player_id = p.player_id

SQL table display error

I am encountering a display issue with my SQL code and was hoping someone could help me figure out whats going on. When I create my CUSTOMER table then INSERT a line of values it works successfully... however, when I type select * from customer; then it displays horrible output where none of the data is lined up in the columns properly. Can you please take a look at my code and tell me what I can do to fix this.
I have multiple tables in this database and none of the other tables have this issue and display properly. My window was configured using these two lines of code:
SET LINESIZE 132
SET PAGESIZE 50
My table creation code:
CREATE TABLE Customer
(
CustomerID NUMBER(5) NOT NULL CONSTRAINT PK_Customer_CustomerID PRIMARY KEY,
BillingID NUMBER(5) NOT NULL,
CustomerFName VARCHAR2(40) NOT NULL,
CustomerLName VARCHAR2(40) NOT NULL,
CustomerPhone VARCHAR2(10) NOT NULL,
CustomerStreet VARCHAR2(30)NOT NULL,
CustomerCity VARCHAR2(30) NOT NULL,
CustomerState CHAR(2) NOT NULL,
CustomerZip VARCHAR2(9) NOT NULL,
CustomerEmail VARCHAR2(75) NOT NULL,
SignUp_Date DATE DEFAULT sysdate NOT NULL,
CustomerStatus CHAR(1) NOT NULL CONSTRAINT CC_Customer_CustomerStatus CHECK (CustomerStatus IN ('A', 'I')),
InactiveDate DATE,
InactiveReason VARCHAR2(200),
CustomerBillingCycle CHAR(1) NOT NULL CONSTRAINT CC_Customer_CustomerBC CHECK (CustomerBillingCycle IN ('A', 'B'))
);
My line of values being inserted into the table:
INSERT INTO Customer VALUES (01234, 99012, 'Michael', 'Huffaker', '6235551414', '65 N 35th Ln', 'Glendale', 'AZ', '85308', 'm.huffaker#quickmail.com', '29-MAY-2010', 'A', NULL, NULL, 'A');
As I stated above, both of these work successfully and the problem appears when I display the data in the table. Check out the screen shot link below to see the messed up output:
http://i.stack.imgur.com/uMu4S.png
It's not messed up at all; the output lines are just "wrapping" the output after 132 characters. It's very normal. I don't use command line often for running selects, but try routing the output to a file. Or perhaps try with a very large LINESIZE setting (like 1000 or so). Your terminal windows may not support a line that wide.