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'
);
Related
im trying to connect 3 tables together, it works just fine however when i try to group them by a certain column it shows me not a group by expression:
my tables:
create table userss (
username varchar2(25)not null ,
password varchar2(25) ,
userTypeID number(8) not null,
gender char(1),
dateCreated date,
lname varchar2(10),
fname varchar2(10),
constraint username1_pk primary key (username),
CONSTRAINT gender1_ck CHECK (gender='M' or gender='F'),
constraint userTypeID_fk foreign key(userTypeID) references userType (userTypeId)
);
create table doctor (
docID number(8) not null ,
docBioData varchar2(50),
username varchar2(25)not null ,
SpecID number(3),
post_id number(5) not null ,
constraint docID_pk primary key(docID),
constraint username4_fk foreign key (username) references userss(username),
constraint specID2_fk foreign key (specID) references speciality(specID),
constraint post_id_fk foreign key (post_id) references positions(post_id)
);
create table rating (
ratID number(10) not null ,
rat_date date,
rat_comment varchar2(100),
rat_rating number(1) not null,
docID number(8) not null ,
patID number(8) not null,
constraint ratID_pk primary key(ratID),
constraint docID_fk foreign key (docID) references doctor(docID),
constraint patID2_fk foreign key (patID) references patient(patID),
constraint rating_ck check (rat_rating between 1 and 5)
);
and my code is:
select d.docid, (fname|| ' '|| lname) "Doctor Name", count(r.rat_rating), avg(r.rat_rating)
from userss u, doctor d, rating r
where u.username = d.username and d.docid = r.docid and rat_date > TO_DATE('01-JAN-2020', 'DD-MON-YYYY')
group by d.docid
order by d.docid desc;
In your SELECT there is 2 columns before the COUNT and the AVG, therefore you have to GROUP BY those 2 columns.
Depending on your version, you cannot group by on an alias, you would need to encapsulate your first query.
select id, Doctor_Name, COUNT(rat_rating), AVG(rat_rating) from (
select d.docid AS id, (fname|| ' '|| lname) AS Doctor_Name, r.rat_rating, r.rat_rating
from userss u, doctor d, rating r
where u.username = d.username and d.docid = r.docid and rat_date > TO_DATE('01-JAN-2020', 'DD-MON-YYYY')
) A
group by id, Doctor_Name
order by id desc
You could on the other hand delete the "Doctor Name" column from your query, and it should work
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;
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'
);
CREATE TABLE Shopper
(
Shopperid INTEGER PRIMARY KEY,
ShopperName VARCHAR2(20) NOT NULL,
Gender VARCHAR2(6) CHECK(Gender IN ('Male', 'Female')),
MobileNo NUMBER NOT NULL,
Address VARCHAR2(50)
);
I'm trying to create a table and I want my mobile no. to be devoid of any hyphens and brackets.
Store the value as a string with a check constraint:
CREATE TABLE Shopper (
Shopperid INTEGER PRIMARY KEY,
ShopperName VARCHAR2(20) NOT NULL,
Gender VARCHAR2(6) CHECK (Gender IN ('Male', 'Female')),
MobileNo VARCHAR2(30) NOT NULL CHECK (REGEXP_LIKE(MobileNo, '^[0-9]*$')),
Address VARCHAR2(50)
);
This is the code posted in the question by Jazir Ahammed
CREATE TABLE Shopper
(
Shopperid INTEGER PRIMARY KEY,
ShopperName VARCHAR2(20) NOT NULL,
Gender VARCHAR2(6) CHECK(Gender IN ('Male', 'Female')),
MobileNo NUMBER NOT NULL,
Address VARCHAR2(50)
);
This is the post by Gordon Linoff
CREATE TABLE Shopper (
Shopperid INTEGER PRIMARY KEY,
ShopperName VARCHAR2(20) NOT NULL,
Gender VARCHAR2(6) CHECK (Gender IN ('Male', 'Female')),
MobileNo VARCHAR2(30) NOT NULL CHECK (REGEXP_LIKE(MobileNo, '^[0-9]$')),
Address VARCHAR2(50)
);
This is the answer for INFYTQ's collaboration assignment 2 on DBMS
CREATE TABLE Shopper(
Shopperid INTEGER,
ShopperName VARCHAR2(20) NOT NULL,
Gender CHAR(6),
MobileNo NUMBER NOT NULL,
Address VARCHAR2(50),
CONSTRAINT s_id_pk PRIMARY KEY(ShopperId),
CONSTRAINT s_gender_ck CHECK(Gender IN ('Male', 'Female'))
)
image of output at InfyTQ
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