Cannot add foreign key SQL fiddle - sql

Im new to sql, I'm having issues with the foreign key in my table. If any can point out the error, please do, I have no idea what it is.enter image description here
CREATE TABLE donut(
donutId int(5) NOT NULL,
name VARCHAR(20),
descriptioin VARCHAR(60),
price DECIMAL(8,3),
PRIMARY KEY (donutId)
);
CREATE TABLE customer(
customerid INT(5)NOT NULL,
firstname VARCHAR(30),
lastname VARCHAR(30),
addres VARCHAR(100),
apt VARCHAR(20),
city VARCHAR(30),
state VARCHAR(20),
zip VARCHAR(8),
homephone VARCHAR(15),
cellphone VARCHAR(15),
otherphone VARCHAR(15),
PRIMARY KEY (customerid) );
CREATE TABLE customer_order_donut(
orderid int(10) NOT NULL,
customerid int(10),
donutId int(10),
orderdate datetime,
donutqty INT(5),
specialhandling TEXT,
PRIMARY KEY (orderid)
customerid int FOREIGN KEY REFERENCES customer(customerid),
donutId int FOREIGN KEY REFERENCES donut(donutId));

Your FOREIGNKEY syntax is wrong. This should work:
CREATE TABLE donut
(
donutid INT(5) NOT NULL,
name VARCHAR(20),
descriptioin VARCHAR(60),
price DECIMAL(8,3),
PRIMARY KEY (donutid)
);
CREATE TABLE customer
(
customerid INT(5)NOT NULL,
firstname VARCHAR(30),
lastname VARCHAR(30),
addres VARCHAR(100),
apt VARCHAR(20),
city VARCHAR(30),
state VARCHAR(20),
zip VARCHAR(8),
homephone VARCHAR(15),
cellphone VARCHAR(15),
otherphone VARCHAR(15),
PRIMARY KEY (customerid)
);
CREATE TABLE customer_order_donut
(
orderid INT(10) NOT NULL,
customerid INT(10),
donutid INT(10),
orderdate DATETIME,
donutqty INT(5),
specialhandling TEXT,
PRIMARY KEY (orderid),
FOREIGN KEY (customerid) REFERENCES customer(customerid),
FOREIGN KEY (donutid) REFERENCES donut(donutid)
);

Check the syntax of the FOREIGN KEY constraints (and add a comma after PRIMARY KEY):
CREATE TABLE customer_order_donut(
orderid int(10) NOT NULL,
customerid int(10),
donutId int(10),
orderdate datetime,
donutqty INT(5),
specialhandling TEXT,
PRIMARY KEY (orderid),
FOREIGN KEY (customerid) REFERENCES customer(customerid),
FOREIGN KEY (donutid) REFERENCES donut(donutId)
);

Related

Dbeaver: Why does code not work correctly?

It doesn't create all my tables and also says I have syntax errors. Can anyone help??
CREATE table project (
project_id int not null,
budget_id int not null,
division_id int not null,
project_name varchar(40),
project_begin_date date,
project_end_date date
);
ALTER table project
add constraint pk_project_project_id
primary key(project_id)
;
ALTER table project
add constraint fk_project_budget_id
foreign key(budget_id) references budget(budget_id)
;
ALTER table project
add constraint fk_project_division_id
foreign key (division_id) references division(division_id)
;
CREATE table division(
division_id int not null,
division_name varchar(40),
constraint pk_division_division_id primary key(division_id)
);
CREATE table committee(
committee id int not null,
committee_name varchar(40),
constraint pk_committee_committee_id primary key(committe_id)
);
create table employee(
employee_id int not null,
given_name varchar(40),
middle_name varchar(40),
family_name varchar(40),
form_of_address varchar(10),
name_suffix varchar(25),
work_phone_number varchar(20),
hourly_budget_rate numeric(8,2),
constraint pk_employee_employee_id primary key(employee_id)
);
CREATE table budget(
budget_id int not null,
budget_description varchar(100),
constraint pk_budget_budget_id primary key(budget_id)
);
CREATE table section(
section_id int not null,
division_id int,
budget_id int,
section_name varchar(40),
constraint pk_section_section_id primary key(section_id),
constraint pk_section_buget_id foreign key(budget_id) references budget(budget_id),
constraint pk_section_division_id foreign key(division_id) references division(division_id)
);
CREATE table employee_job_assignment(
job_assignment_id int not null,
employee_id int not null,
section_id int not null,
begin_datetime date,
end_datetime date,
job_title varchar(100),
constraint pk_employee_job_assignment primary key(job_assignment_id),
constraint pk_employee_job_assignment foreign key(employee_id) references employee(employee_id),
constraint pk_employee_job_assignment foreign key(section_id) references section(section_id)
);
CREATE table budget_category(
budget_category_code char(5),
budget_category_description char(100),
constraint pk_budget_category primary key(budget_category_code)
);
CREATE table budget_charge(
transaction_id int not null,
budget_category_code char(5),
budget_id int not null,
transaction_date date,
transaction_description varchar(100),
transaction_type char(5),
constraint pk_budget_charge_transaction_id primary key(transaction_id),
constraint pk_budget_charge_budget_category_code foreign key(budget_category_code) references budget_category(budget_category_code),
constraint pk_budget_charge_budget_id foreign key(budget_id) references budget(budget)
);
CREATE office(
office_id int not null,
office_type char(1),
office_building char(5),
office_location varchar(20),
constraint pk_office primary key(office_id)
);
CREATE table employee_office(
office id int not null,
employee_id int not null,
constraint pk_employee_office_office_id foreign key(office_id) references office(office_id),
constraint pk_employee_office_employee_id foreign key(employee id) references employee(employee_id)
);
CREATE table project_employee(
employee_id int not null,
project_id int not null,
project_role varchar(25),
constraint pk_project_employee_employee_id foreign key(employee_id) references employee(employee_id),
constraint pk_project_employee_project_id foreign key(project_id) references project(project_id)
);
CREATE table purchase_expense_charge(
transaction_id int not null,
purchase_expense_dollars numeric(8,2),
constraint pk_purchase_expense_charge_transaction_id primary key(transaction_id),
constraint pk_purchase_expense_charge_transaction_id foreign key(transaction_id) references budget_charge(transaction_id)
);
CREATE table hours_charge(
transaction_id int not null,
employee_id int not null,
hours_charged numeric(7,2),
time_period_begin_date date,
time_period_end_date date,
constraint pk_hours_charge_transaction_id primary key(transaction_id),
constraint pk_hours_charge_transaction_id foreign key(transaction_id) references budget_charge(transaction_id),
constraint pk_hours_charge_employee_id foreign key(employee_id) references employee(employee_id)
);
CREATE table budget_line_item(
budget_category_code char(5),
budget_id int not null,
line_item_budget_dollars numeric(8,2),
constraint pk_budget_line_item_budget_category_code foreign key(budget_category_code) references budget_category(budget_category_code),
constraint pk_budget_line_item_budget_category_code foreign key(budget_id) references budget(budget_id)
);
CREATE table travel_expense_charge(
transaction_id int not null,
employee_id int not null,
trip_expense_dollars NUMERIC(8,2),
trip_destination VARCHAR(100),
trip_begin_date date,
trip_end_date date,
constraint pk_travel_expense_charge_transaction_id primary key (transaction_id),
constraint pk_travel_expense_charge_transaction_id foreign key (transaction_id) references budget_charge(transaction_id),
constraint pk_travel_expense_charge_employee_id foreign key (employee_id) references employee(employee_id)
);
CREATE table committee_member(
committee_id int not null,
employee_id int not null,
committee_role varchar(25),
constraint pk_committee_member_committee_id foreign key(committee_id) references committee(committee_id)
constraint pk_comittee_member_employee_id foreign key(employee id) references employee(employee_id)
);

Error ORA-00904 in SQL developer. Seemly no fix online that works that I can find while creating a table

I have seen many posts regarding the seemingly infamous ORA-00904 yet nothing seems to be helping. I am creating 2 tables (for now, more to come later) which are as follows;
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY (customerID) REFERENCES Customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
);
and
CREATE TABLE Customer(
CustomerID INT NOT NULL PRIMARY KEY,
ProjectID INT NOT NULL,
FOREIGN KEY(ProjectID) REFERENCES Project(ProjectID), /*This is yet another table with a foreign key which needs to be sorted*/
CustomerName Char(255) NOT NULL,
PhoneNumber INT NOT NULL,
Region CHAR(255) NOT NULL
);
but every time I run it, I get error;
Error starting at line : 4 in command -
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY(customerID) REFERENCES Customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
Error report -
ORA-00904: "CUSTOMERID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
ANY HELP WOULD BE GREATLY APPRECIATED!
Oracle by default converts all names to UpperCase if you don't enquote them. (Edit: At the time of this answer the Foreign Key Constraint in the starting post did contain a quoted column name).
So, your constraint tries to find a column named "customerID" whereas the real name of your column is "CUSTOMERID".
Either enquote your column:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
"customerID" INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY ("customerID") REFERENCES customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
Don't use quotes on your FOREIGN KEY constraint:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY (customerID) REFERENCES customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
Or use quotes and write it uppercase in your foreign key constraint:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY ("CUSTOMERID") REFERENCES customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
As already mentioned, keeping the style consistent is recommended to prevent future problems, so Option 2 would be the easiest solution. Otherwise you need to enquote all columns to be consistent.
/Edit:
I just tried it - this definitely works with Oracle 12C:
CREATE TABLE Customer (
CustomerID INT,
PRIMARY KEY (CustomerID)
)
Followed by:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY(customerID) REFERENCES Customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
The referenced table needs to created before the foreign key reference. One method is to create the tables first and then add the foreign key references:
CREATE TABLE Employee (
EmployeeID int NOT NULL,
PRIMARY KEY (EmployeeID),
customerID INT NOT NULL,
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
);
CREATE TABLE Customer (
CustomerID INT NOT NULL PRIMARY KEY,
ProjectID INT NOT NULL,
-- FOREIGN KEY(ProjectID) REFERENCES Project(ProjectID), /*This is yet another table with a foreign key which needs to be sorted*/
CustomerName Char(255) NOT NULL,
PhoneNumber INT NOT NULL,
Region CHAR(255) NOT NULL
);
alter table Employee add CONSTRAINT employee_customer_fk FOREIGN KEY (customerID) REFERENCES Customer (CustomerID)
Here is a db<>fiddle.
Note that I commented out the foreign key constraint to Project.
Also, in most data models you can just order the table creates -- there are no cycles in the foreign key "linkages". However, because you have not defined Project, I am suggesting this more general approach.

assignment_code needs to be unique but I dont want it to

I have the following table
CREATE TABLE assignment (
assignment_code VARCHAR(10),
course_code VARCHAR(10),
PRIMARY KEY(course_code, assignment_code),
FOREIGN KEY(course_code) REFERENCES course(course_code));
But when I try to create it PostgreSQL tells me assignment_code needs to be unique. However my assignment is to make assignment_code unique per course in stead of completely unique, how do I do that?
the code below creates the enitre database (no data)
CREATE TABLE students (
student_id BIGINT PRIMARY KEY,
first_name VARCHAR(15),
surname VARCHAR(35),
enrollment_year INT,
bsn BIGINT,
class VARCHAR(5),
Graduated BOOLEAN);
CREATE TABLE teachers (
bsn BIGINT PRIMARY KEY,
first_name VARCHAR(15),
surname VARCHAR(35),
salary REAL,
scale INT,
CONSTRAINT salary CHECK(salary < (25 * scale) AND salary > (20 * scale)));
CREATE TABLE course (
course_code VARCHAR(10) PRIMARY KEY,
course_name VARCHAR(20),
course_follower VARCHAR(5),
study_points INT,
Amount_of_assignments INT);
CREATE TABLE teaches (
bsn BIGINT,
class VARCHAR(5),
course_code VARCHAR(10),
PRIMARY KEY(bsn, class, course_code),
FOREIGN KEY(bsn) REFERENCES teachers(bsn),
FOREIGN KEY(course_code) REFERENCES course(course_code));
CREATE TABLE study_program (
course_name VARCHAR(20),
course_code VARCHAR(10) PRIMARY KEY,
level VARCHAR(15),
duration VARCHAR(10),
FOREIGN KEY(course_code) REFERENCES course(course_code));
CREATE TABLE assignment (
assignment_code VARCHAR(10),
course_code VARCHAR(10),
PRIMARY KEY(course_code, assignment_code),
FOREIGN KEY(course_code) REFERENCES course(course_code));
CREATE TABLE records (
student_id BIGINT,
course_code VARCHAR(20),
school VARCHAR(50),
PRIMARY KEY(student_id, course_code),
FOREIGN KEY(student_id) REFERENCES students(student_id),
FOREIGN KEY(course_code) REFERENCES study_program(course_code));
CREATE TABLE make (
student_id BIGINT,
assignment_code VARCHAR(20),
completed BOOLEAN,
PRIMARY KEY(student_id, assignment_code),
FOREIGN KEY(student_id) REFERENCES students(student_id),
FOREIGN KEY(assignment_code) REFERENCES assignment(assignment_code));
CREATE TABLE prerequisit (
assignment_code VARCHAR(20),
prerequisit_code VARCHAR(20),
course_code VARCHAR(20),
PRIMARY KEY(assignment_code, course_code),
FOREIGN KEY(assignment_code) REFERENCES assignment(assignment_code),
FOREIGN KEY(course_code) REFERENCES course(course_code),
FOREIGN KEY(prerequisit_code) REFERENCES assignment(assignment_code));
CREATE TABLE records_2 (
assignment_code VARCHAR(20),
course_code VARCHAR(20),
bsn BIGINT,
mandatory BOOLEAN,
year INT,
week INT,
PRIMARY KEY(assignment_code, course_code, bsn),
FOREIGN KEY(assignment_code) REFERENCES assignment(assignment_code),
FOREIGN KEY(course_code) REFERENCES course(course_code),
FOREIGN KEY(bsn) REFERENCES teachers(bsn));
CREATE TABLE designes (
course_code VARCHAR(15),
bsn BIGINT,
PRIMARY KEY(course_code),
FOREIGN KEY(course_code) REFERENCES course(course_code),
FOREIGN KEY(bsn) REFERENCES teachers(bsn));
CREATE TABLE reviews (
bsn BIGINT,
course_code VARCHAR(15),
PRIMARY KEY(bsn, course_code),
FOREIGN KEY(bsn) REFERENCES teachers(bsn),
FOREIGN KEY(course_code) REFERENCES course(course_code));
Lurking deep in your code is this:
CREATE TABLE records_2
. . .
FOREIGN KEY(assignment_code) REFERENCES assignment(assignment_code),
A foreign key reference needs to be to a unique or primary key. If you have a composite primary key, then you need to use all the keys. Hence, this reference is missing course_code.
I am a strong believer in synthetic primary keys -- that is, serial integer columns that provide this information. I would recommend that you use them in your table and for foreign key references.

Oracle Problems with Composite Keys

bit confusing question here:
I am trying to create a composite FK from a composite PK. I'll show you my tables in question that I'm having problems with;
CREATE TABLE Weapons (
Weapon_ID VARCHAR(10) NOT NULL,
Weapon_Name VARCHAR(30),
Range_In_Meters INT,
Maximum_Number_Of_Uses INT,
Damage_Factor INT,
Cost INT,
primary key(Weapon_ID));
CREATE TABLE WeaponInventory (
Inventory_ID VARCHAR(30) NOT NULL,
WeaponInventory_ID Varchar(10) NOT NULL,
Weapon_ID VARCHAR(10) REFERENCES Weapons(Weapon_ID) NOT NULL,
primary key(Inventory_ID, WeaponInventory_ID));
CREATE TABLE Avatars (
Avatar_ID VARCHAR(10) NOT NULL,
Avatar_Name VARCHAR(30),
AvA_DOB DATE,
Age VARCHAR(30),
Gender VARCHAR(30),
Strength_Indicated INT,
Hoard INT,
Avatar_Level VARCHAR(30),
Skill VARCHAR(30),
Original_Owner VARCHAR(30),
Family_ID VARCHAR(10) NOT NULL,
Species_ID VARCHAR(10) NOT NULL,
Inventory_ID VARCHAR(30) NOT NULL,
Weapon_ID VARCHAR(10) NOT NULL,
Player_ID VARCHAR(10) NOT NULL,
PRIMARY KEY (Avatar_ID),
FOREIGN KEY (Inventory_ID, Weapon_ID)
REFERENCES WeaponInventory (Inventory_ID, Weapon_ID));
In the table Avatars, I have been trying to create this Foreign key but I'm having no luck. I have errors such as:
ORA-02270: no matching unique or primary key for this column-list
and:
ORA-02270: no matching unique or primary key for this column-list
Any help would be greatly appreciated! I just keep getting confused and ending up in the same place! (:
I think you planned to reference column WeaponInventory_ID (instead of Weapon_ID) in table WeaponInventory:
FOREIGN KEY (Inventory_ID, Weapon_ID)
REFERENCES WeaponInventory (Inventory_ID, WeaponInventory_ID) -- see here
Note that Weapon_ID is not part of the PK in WeaponInventory:
CREATE TABLE WeaponInventory (
Inventory_ID VARCHAR(30) NOT NULL,
WeaponInventory_ID Varchar(10) NOT NULL,
Weapon_ID VARCHAR(10) REFERENCES Weapons(Weapon_ID) NOT NULL,
primary key(Inventory_ID, WeaponInventory_ID) -- Weapon_ID is not part of PK
);
Check this Fiddle.

Need help in the query (oracle 11 g)

First of all, these are my create_table statements.
/*--- Base tables ---*/
CREATE TABLE CUSTOMER (
CUST_ID VARCHAR(10) NOT NULL,
CUST_FNAME VARCHAR(15),
CUST_LNAME VARCHAR(15),
CUST_HP NUMBER(10),
CUST_EMAIL VARCHAR(40),
primary key (CUST_ID)
);
CREATE TABLE STAFF(
STAFF_ID NUMBER(12) NOT NULL,
STAFF_FNAME VARCHAR(15),
STAFF_LNAME VARCHAR(15),
STAFF_DOB Date,
STAFF_AGE NUMBER(2),
STAFF_ADDRESS VARCHAR(70),
STAFF_HP NUMBER(10),
STAFF_SALARY NUMBER(6,2),
primary key (STAFF_ID)
);
CREATE TABLE SUPPLIER(
SUPP_ID VARCHAR(10) NOT NULL,
SUPP_NAME VARCHAR(30),
SUPP_TEL NUMBER(10),
SUPP_EMAIL VARCHAR(25),
SUPP_ADDRESS VARCHAR(80),
SUPP_CITY VARCHAR(20),
SUPP_STATE VARCHAR(10),
SUPP_ZIP NUMBER(5),
primary key (SUPP_ID)
);
CREATE TABLE PRODUCT(
PROD_ID VARCHAR(10) NOT NULL,
PROD_NAME VARCHAR(30),
PROD_DESC VARCHAR(70),
R_UNIT_PRICE NUMBER(3,2) DEFAULT 4.50,
L_UNIT_PRICE NUMBER(3,2) DEFAULT 5.00,
primary key (PROD_ID)
);
CREATE TABLE INGREDIENT(
ING_ID VARCHAR(10) NOT NULL,
ING_NAME VARCHAR(20),
ING_DESC VARCHAR(60),
primary key (ING_ID)
);
CREATE TABLE TOPPING(
TOP_ID VARCHAR(10) NOT NULL,
TOP_NAME VARCHAR(20),
TOP_DESC VARCHAR(40),
TOP_PRICE NUMBER(2,2),
primary key (TOP_ID)
);
/*--- Child tables ---*/
CREATE TABLE ORDERS(
ORDERS_ID VARCHAR(10) NOT NULL,
CUST_ID VARCHAR(10) NOT NULL,
STAFF_ID NUMBER(12) NOT NULL,
TOTAL_PRICE NUMBER(5,2),
ORDERS_DATE DATE,
primary key (ORDERS_ID),
foreign key (CUST_ID) references CUSTOMER(CUST_ID),
foreign key (STAFF_ID) references STAFF(STAFF_ID)
);
CREATE TABLE ORD_DETAIL(
SK1_ID NUMBER(4) NOT NULL,
ORDERS_ID VARCHAR(10) NOT NULL,
PROD_ID VARCHAR(10) NOT NULL,
TOP_ID VARCHAR(10) NOT NULL,
TEATYPE VARCHAR(8),
CUPSIZE CHAR(1),
QTY NUMBER(2),
CONSTRAINT CHK_CUPSIZE CHECK (CUPSIZE = 'R' OR CUPSIZE = 'L'),
CONSTRAINT CHK_TEATYPE CHECK (TEATYPE = 'Oolong' OR TEATYPE = 'Jasmine'),
primary key (SK1_ID),
foreign key (ORDERS_ID) references ORDERS(ORDERS_ID),
foreign key (PROD_ID) references PRODUCT(PROD_ID),
foreign key (TOP_ID) references TOPPING(TOP_ID)
);
CREATE TABLE PROD_ING(
PROD_ID VARCHAR(10) NOT NULL,
ING_ID VARCHAR(10) NOT NULL,
primary key (PROD_ID , ING_ID),
foreign key (PROD_ID) references PRODUCT(PROD_ID),
foreign key (ING_ID) references INGREDIENT(ING_ID)
);
CREATE TABLE ING_SUPP(
ING_ID VARCHAR(10) NOT NULL,
SUPP_ID VARCHAR(10) NOT NULL,
SUPP_PRICE NUMBER(6,2),
primary key (ING_ID, SUPP_ID),
foreign key (ING_ID) references INGREDIENT(ING_ID),
foreign key (SUPP_ID) references SUPPLIER(SUPP_ID)
);
And my query is this:Which employee served the most number of customers?
But when I executed these statements:
SELECT MAX (COUNT (O.CUST_ID)) AS "Served customer"
FROM STAFF S, ORDERS O
WHERE (S.STAFF_ID = O.STAFF_ID)
GROUP BY O.STAFF_ID
HAVING COUNT (O.CUST_ID) > 0;
What I got was there was a column named "Served customer" with the maximum number only.
So, now my question is how to display the employee's ID, first name and last name along with
"Served customer" column.
The question is: "Which employee served the most number of customers? " Your query does not even return information about the employee, only about the number of customers.
The following aggregation returns for each staff member, the number of customers who were served:
SELECT O.STAFF_ID, COUNT(O.CUST_ID) AS "Served customer"
FROM STAFF SS JOIN
ORDERS O
ON S.STAFF_ID = O.STAFF_ID
GROUP BY O.STAFF_ID;
To get the maximum number we just need to order this by the count and take the first row. In Oracle, this uses a subquery:
SELECT so.*
FROM (SELECT O.STAFF_ID, COUNT(O.CUST_ID) AS "Served customer"
FROM STAFF SS JOIN
ORDERS O
ON S.STAFF_ID = O.STAFF_ID
GROUP BY O.STAFF_ID
ORDER BY COUNT(O.CUST_ID) DESC
) so
WHERE rownum = 1;