Select from 3 tables with IN clause - sql

Need to display next: all fNAME and lNAME from table username that have position of "Admin" and work in 'ABC' company (NAME_COMPANY), with IN clause.
create table company
(
CODE_COMPANY char(30),
NAME_COMPANY varchar2(30) not null,
MAIL_COMPANY varchar2(30) null,
constraint PK_CODE_COMPANY primary key (CODE_COMPANY),
);
create table USERNAME
(
NAME_USERNAME varchar2(30),
USER_LOCATION number,
fNAME varchar2 (30) not null,
lNAME varchar2 (30) not null,
PHONE_USER char(13) null,
USER_POSITION varchar2 (30),
check (USER_POSITION in('Admin', 'Superadmin', 'Technician', 'Student')),
constraint PK_NAME_USERNAME primary key (NAME_USERNAME),
constraint FK_USER_LOCATION foreign key (USER_LOCATION) references uLOCATION (LOCATION)
);
create table uLOCATION
(
LOCATION number,
CODE_COMPANY char(30),
NAME_LOCATION varchar2(30) not null,
FLOOR_LOCATION varchar2(10),
check (FLOOR_LOCATION in ('MAIN_FLOOR', '1ST FLOOR', '2ND FLOOR', '3RD FLOOR')),
constraint PK_LOCATION primary key (LOCATION),
constraint FK_CODE_COMPANY_L foreign key (CODE_COMPANY) references company (CODE_COMPANY),
);

If I understand your question correctly, you want the following query. Not sure why you would use an IN instead of a standard = however I've included both with one commented.
select
user.fName,
user.lName
from
username as user
inner join ulocation as location
on location.location = user.user_location
inner join company as company
on company.code_company = location.code_company
where
user.user_position = 'Admin'
and name_company in ('A','B','C') -- not needed if only checking for one company
-- if only one company, change to: name_company = 'ABC'

I think you are looking for:
select u.*
from username u
where u.user_position = 'admin' and
u.ulocation in (select l.location
from ulocation l join
ucompany c
on l.code_company = c.code_company
where c.name_company = 'ABC'
);

Related

SQL Select Query Giving me duplicate columns

I am trying to write a query to display information, but when I use INNER JOIN I am getting duplicate ID fields. My tables and query are below. I am getting duplicate columns for wrestlerID. could it be my PK constraints?
CREATE TABLE WRESTLER
(
WrestlerID CHAR(6) NOT NULL,
WrestlerFirst VARCHAR2(15) NOT NULL,
WrestlerLast VARCHAR2(25) NOT NULL,
WrestlerStyle CHAR(2) NOT NULL,
WrestleKilos NUMERIC(3,0) NOT NULL,
WrestleMeters NUMERIC(3,2),
WrestleCity VARCHAR2(40) NOT NULL,
WrestlerState CHAR(2) NOT NULL,
WrestlerBirthdate Date,
CONSTRAINT WRESTLER_PK PRIMARY KEY (WrestlerID)
);
CREATE TABLE CLUB
(
ClubID CHAR(5) NOT NULL,
Club VARCHAR2(35) NOT NULL,
WrestlerID CHAR(6) NOT NULL,
CONSTRAINT CLUB_PK PRIMARY KEY (ClubID, WrestlerID),
CONSTRAINT CLUB_FK
FOREIGN KEY (WrestlerID) REFERENCES WRESTLER(WrestlerID)
);
CREATE TABLE SCHOOL
(
SchoolID VARCHAR2(10) NOT NULL,
School VARCHAR2(35) NOT NULL,
WrestlerID CHAR(6) NOT NULL,
CONSTRAINT SCHOOL_PK PRIMARY KEY (SchoolID, WrestlerID),
CONSTRAINT SCHOOL_FK
FOREIGN KEY (WrestlerID) REFERENCES WRESTLER(WrestlerID)
);
CREATE TABLE MEDAL
(
WrestlerID CHAR(6) NOT NULL,
WorldMedal NUMERIC(4,0),
CONSTRAINT MEDAL_PK PRIMARY KEY (WrestlerID, WorldMedal),
CONSTRAINT MEDAL_FK
FOREIGN KEY (WrestlerID) REFERENCES WRESTLER(WrestlerID)
);
SELECT
t1.WrestlerID, t1.WrestlerFirst, t1.WrestlerLast,
t2.WrestlerID, t2.School,
t2.WrestlerID, t3.Club
FROM
WRESTLER t1
INNER JOIN
SCHOOL t2 ON t1.WrestlerID = t2.WrestlerID
INNER JOIN
CLUB t3 ON t1.WrestlerID = t3.WrestlerID;
Fixing my query worked
SELECT
t1.WrestlerID, t1.WrestlerFirst, t1.WrestlerLast,
t2.School,
t3.Club FROM
WRESTLER t1 INNER JOIN
SCHOOL t2 on t1.WrestlerID = t2.WrestlerID INNER JOIN
CLUB t3 on t1.WrestlerID = t3.WrestlerID;

Select from 3 tables with foreign key

Need to display next: all fNAME and lNAME from table username that have position of "Admin" from location "3RD Floor", with IN clause
create table company
(
CODE_COMPANY char(30),
NAME_COMPANY varchar2(30) not null,
MAIL_COMPANY varchar2(30) null,
constraint PK_CODE_COMPANY primary key (CODE_COMPANY),
);
create table USERNAME
(
NAME_USERNAME varchar2(30),
USER_LOCATION number,
fNAME varchar2 (30) not null,
lNAME varchar2 (30) not null,
PHONE_USER char(13) null,
USER_POSITION varchar2 (30),
check (USER_POSITION in('Admin', 'Superadmin', 'Technician', 'Student')),
constraint PK_NAME_USERNAME primary key (NAME_USERNAME),
constraint FK_USER_LOCATION foreign key (USER_LOCATION) references uLOCATION (LOCATION)
);
create table uLOCATION
(
LOCATION number,
CODE_COMPANY char(30),
NAME_LOCATION varchar2(30) not null,
FLOOR_LOCATION varchar2(10),
check (FLOOR_LOCATION in ('MAIN_FLOOR', '1ST FLOOR', '2ND FLOOR', '3RD FLOOR')),
constraint PK_LOCATION primary key (LOCATION),
constraint FK_CODE_COMPANY_L foreign key (CODE_COMPANY) references company (CODE_COMPANY),
);
SELECT U.fName, U.lName
FROM USERNAME AS U
WHERE U.USER_POSITION = 'Admin'
AND
U.USER_LOCATION IN (
SELECT L.LOCATION
FROM uLOCATION AS L
WHERE L.FLOOR_LOCATION = '3RD FLOOR'
);
select fName, lName
from USERNAME, uLOCATION, COMPANY
where uLOCATION.LOCATION = USERNAME.USER_LOCATION and COMPANY.CODE_COMPANY = uLOCATION.CODE_COMPANY;
But how to specify, that I need user Admin from NAME_COMPANY ABC?
how to add 2 more WHERE clause?
Slight modifying SQLRaptor's answer for NAME_COMPANY:
SELECT U.fName, U.lName
FROM USERNAME AS U
WHERE U.USER_POSITION = 'Admin'
AND
U.USER_LOCATION IN (
SELECT L.LOCATION
FROM uLOCATION AS L JOIN COMPANY AS C
ON L.CODE_COMPANY=C.CODE_COMPANY
WHERE L.FLOOR_LOCATION = '3RD FLOOR' AND C.NAME_COMPANY = 'ABC'
);

sqlplus using of sub query

I have 3 tables, and have to find out the applicant number, name and total number of position each applicant applied for.
CREATE TABLE APP (
appNum varchar2(10) not null,
appName varchar2(70),
constraint applicant_Pkey primary key (appNum)
);
CREATE TABLE POS (
posNum varchar2(10) not null,
posStartOfferDt date not null,
constraint pos_Pkey primary key (posNum, posStartOfferDt)
);
CREATE TABLE APPLICATION (
appcnPosNum varchar2(10) not null,
appcnPosStOffrDt date not null,
appcnAppNum varchar2(10) not null,
appcnDt date,
constraint application_Pkey primary key (appcnPosNum, appcnPosStOffrDt, appcnAppNum),
constraint application_Fkey1 foreign key (appcnPosNum, appcnPosStOffrDt) references POSITION(posNum, posStartOfferDt),
constraint application_Fkey2 foreign key (appcnAppNum) references APPLICANT(appNum)
);
I have tried using sub query, natural join but all not working out for me.
SELECT appNum, appName, COUNT(*)
FROM applicant
JOIN application ON applicant.appNum = application.appcnAppNum
GROUP BY appNum, appName

Need help with some Oracle SQL Queries

Here is the question posed by a professor: Find the minimum and maximum length of movies playing in each city.
And here is how I have my tables structured:
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've tried a few different queries, but keep getting issues. Here is what I have:
SELECT MIN(Movies.Length), MAX(Movies.Length), Theatres.Name
FROM Theatres, Movies, ShownAt
WHERE ShownAt.TheatreName = Theatres.Name AND
ShownAt.MovieTitle = Movies.Title AND
Theatres.City IN (SELECT UNIQUE City FROM Theatres);
Anybody see anything wrong? Thanks for the help!
You were pretty close I think. Just missing GROUP BY
SELECT
MIN(Movies.Length) AS Shortest,
MAX(Movies.Length) AS Longest,
Theatres.City
FROM Theatres
JOIN ShownAt ON ShownAt.TheatreName = Theatres.Name
JOIN Movies ON ShownAt.MovieTitle = Movies.Title
GROUP BY Theatres.City
I believe this would do the trick:
SELECT T.City, MIN(M.Length) AS MinLength, MAX(M.Length) AS MaxLength
FROM Movies AS M
JOIN ShownAt AS S ON S.MovieTitle = M.Title
JOIN Theatres AS T ON S.TheatreName = T.Name
GROUP BY T.City

Where clause in Mutiple table sql joins

TABLES
CREATE TABLE LocalBusiness
(
BusinessID INT NOT NULL PRIMARY KEY,
BusinessName VARCHAR2 (20) NOT NULL,
TypeID INT,
Latitude DECIMAL (10,2),
Longitude DECIMAL (10,2),
Web_address VARCHAR2 (50) NOT NULL,
Postcode VARCHAR2 (10) NOT NULL,
official_rating int,
min_price NUMBER(4,2),
max_price NUMBER(4,2),
FOREIGN KEY (TypeID) REFERENCES LocalBusinessType (TypeID),
CONSTRAINT chk_Officialrating CHECK (official_rating> 0 AND official_rating<6 )
);
CREATE TABLE Address
(
AddressID INT NOT NULL PRIMARY KEY,
BusinessID INT,
AreaID INT,
Address VARCHAR2 (50) NOT NULL,
Postcode VARCHAR2 (10) NOT NULL,
FOREIGN KEY (BusinessID) REFERENCES LocalBusiness (BusinessID),
FOREIGN KEY (AreaID) REFERENCES Area (AreaID)
);
CREATE TABLE Phone
(
PhoneNoID INT NOT NULL PRIMARY KEY,
PhoneNo VARCHAR2 (15) NOT NULL,
BusinessID INT,
Description VARCHAR2 (50) NOT NULL,
FOREIGN KEY (BusinessID) REFERENCES LocalBusiness (BusinessID),
CONSTRAINT PhoneNo_unique UNIQUE (PhoneNo)
);
CREATE TABLE Email
(
EmailID INT NOT NULL PRIMARY KEY,
email_address VARCHAR2 (50) NOT NULL,
BusinessID INT,
Description VARCHAR2 (50) NOT NULL,
FOREIGN KEY (BusinessID) REFERENCES LocalBusiness (BusinessID),
CONSTRAINT email_unique UNIQUE (email_address)
);
CREATE TABLE Area
(
AreaID INT NOT NULL PRIMARY KEY,
AreaName VARCHAR2 (20) NOT NULL,
Region VARCHAR2 (20) NOT NULL
);
SELECT statement:
SELECT
LocalBussiness.BusinessName, Address.Address, Address.Postcode,
Area.AreaName, Area.Region, LocalBusiness.OfficialRating,
LocalBusiness.min_price, LocalBusiness_max_price,
Phone.description, Phone.PhoneNo,
Email.Description, Email.email_address,
LocalBusiness.Web_address
FROM
LocalBusiness
JOIN
Address ON LocalBusiness.BusinessID = Address.BusinessID
JOIN
Area ON Address.AreaID = Area.AreaID
AND LocalBusiness.BusinessID = Address.BusinessID
JOIN
Phone ON Phone.BusinessID = LocalBusiness.BusinessID
JOIN
Email ON Email.BusinessID = LocalBusiness.BusinessID
WHERE
TypeID = '1'
ORDER BY
LocalBusiness.BusinessName ASC;
The where clause in the sql join statement written above seem to be ineffective as the some values for the TypeID return incomplete data while others return no rows at all. How do I go about fixing this?
Yyou repeat a condition
AND LocalBusiness.BusinessID=Address.BusinessID in JOIN Area
Try avoinding this repetition
SELECT LocalBussiness.BusinessName, Address.Address, Address.Postcode,
Area.AreaName, Area.Region, LocalBusiness.OfficialRating,
LocalBusiness.min_price, LocalBusiness_max_price, Phone.description,
Phone.PhoneNo, Email.Description, Email.email_address,
LocalBusiness.Web_address
FROM LocalBusiness
JOIN Address ON LocalBusiness.BusinessID=Address.BusinessID
JOIN Area ON Address.AreaID=Area.AreaID
JOIN Phone ON Phone.BusinessID=LocalBusiness.BusinessID
JOIN Email ON Email.BusinessID=LocalBusiness.BusinessID
WHERE TypeID = '1'
ORDER BY LocalBusiness.BusinessName ASC;
Otherwise check th consistence of your data ..