I have used W3school and my in class notes. Was wondering if someone could point me in the right direction.
I am working on an SQL peoject.
I have a database that made from three tables employees, customers, and customer orders.
CUSTOMER (
CUST_NBR NUMBER(10) NOT NULL ,
FNAME NVARCHAR2(20) NULL,
LNAME NVARCHAR2(20) NULL,
PRIMARY KEY(CUST_NBR)
);
EMPLOYEE (
EMP_ID NUMBER(10) NOT NULL ,
FNAME NVARCHAR2(20) NULL,
LNAME NVARCHAR2(20) NULL,
MANAGER_EMP_ID NUMBER(10) NULL,
PRIMARY KEY(EMP_ID),
FOREIGN KEY(MANAGER_EMP_ID)
REFERENCES EMPLOYEE(EMP_ID)
);
CUST_ORDER (
ORDER_NBR NUMBER(10) NOT NULL ,
CUST_NBR NUMBER(10) NOT NULL,
SALES_EMP_ID NUMBER(10) NOT NULL,
SALE_PRICE NUMBER(10, 2) NULL,
PRIMARY KEY(ORDER_NBR),
FOREIGN KEY(SALES_EMP_ID)
REFERENCES EMPLOYEE(EMP_ID),
FOREIGN KEY(CUST_NBR)
REFERENCES CUSTOMER(CUST_NBR)
);
I have filled in sample data for all tables. I am looking to write a query that will allow me to return information fname, lname, order_nbr for the item with the lowest sale price.
I have tried several variations of
Select *
FROM CUST_ORDERS
WHERE SALE_PRICE = min(Sale_price):
Any help would be greatly appreciated.
SELECT TOP 1 * FROM CUST_ORDERS ORDER BY SALE_PRICE;
SELECT TOP 1
fname, lname, order_nbr
FROM cust_order co
JOIN customer с
ON c.cust_nbr = co.cust_nbr
ORDER BY
co.sale_price
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
assuming these 3 tables
create table item(
item_id integer NOT NULL primary key,
name varchar(50) NOT NULL,
description varchar(150) NOT NULL,
in_stock integer NOT NULL
)
create table customer(
customer_id VARCHAR(9) NOT NULL primary key,
name VARCHAR(50) NOT NULL,
lastname VARCHAR(50) NOT NULL,
phone VARCHAR(15) NOT NULL,
join_date DATE NOT NULL
)
create table purchase(
purchase_id integer references item,
customer_id varchar(9) references customer,
purchase_date TIMESTAMP NOT NULL,
amount INTEGER NOT NULL,
PRIMARY KEY(purchase_id, customer_id, purchase_date)
)
how could I get each unique name and the total amount of items purchased?
how could I get each purchase name and the buyer's name and lastname?
how could I get each item and how many of it were sold?
The two topics you are looking to learn are how to use GROUP BY and how to JOIN tables. Here's an example (more or less) that answers your first question and uses both tools:
select
C.customer_id as customer_id,
max(C.name) as customer_name,
sum(amount) as total_amount
from customer C
left join purchase P on C.customer_id = P.customer_id
group by C.customer_id
CREATE TABLE EMPLOYEE (
EID CHAR(3) NOT NULL PRIMARY KEY,
ENAME VARCHAR2(50) NOT NULL,
JOB_TYPE VARCHAR2(50) NOT NULL,
MANAGER CHAR(3) FOREIGN KEY REFERENCES EMPLOYEE(EID),
HIRE_DATE DATE NOT NULL,
DNO INTEGER FOREIGN KEY REFERENCES DEPARTMENT(DNO),
COMMISSION DECIMAL(10,2),
SALARY DECIMAL(7,2) NOT NULL,
);
CREATE TABLE DEPARTMENT (
DNO INT NOT NULL PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50) DEFAULT('NEW DELHI')
);
in creation of the employee table
this is giving me an error of right parenthesis
and the department table is already created
You have an extra comma in the line
SALARY DECIMAL(7,2) NOT NULL,
Delete that comma and the Employee table should be created.
You need to Create the Department Table first to use one of its
Columns as FOREIGN KEY.
Also, check your database. There might already be a Department Table. To avoid getting that error when the table needed is already created, use the keyword IF NOT EXISTS
CREATE TABLE IF NOT EXISTS Department(
DNO INT NOT NULL PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50) DEFAULT('NEW DELHI')
);
The error is caused by the trailing comma, but you have other issues:
The FOREIGN KEY is not needed for an inline reference.
You need to define the tables in the right order.
So . . .
CREATE TABLE DEPARTMENT (
DNO INT NOT NULL PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50) DEFAULT('NEW DELHI')
);
CREATE TABLE EMPLOYEE (
EID CHAR(3) NOT NULL PRIMARY KEY,
ENAME VARCHAR2(50) NOT NULL,
JOB_TYPE VARCHAR2(50) NOT NULL,
MANAGER CHAR(3) REFERENCES EMPLOYEE(EID),
HIRE_DATE DATE NOT NULL,
DNO INTEGER REFERENCES DEPARTMENT(DNO),
COMMISSION DECIMAL(10,2),
SALARY DECIMAL(7,2) NOT NULL
);
Here is an example of it working.
Currently working on SSH secure shell code that keeps giving this error code. I've tried changing the table names but still, I get this error. HELP!
Here's what the questions ask:
List the product ID, product name, and product price for the product(s) sold in more orders than any any other products (based on number of order occurrences, not the quantity). Format the price as currency, and use the following column headings: ProductID, Name, Price.
List the category ID, product ID, product name, and product price for the lowest priced product in each category. Format the price as currency, and use the following column headings: Cat_ID, Prod_ID, Prod_Name, Price.
Here's what the code with the errors:
SQL> --question 15
SQL>
SQL> SELECT Pro.ProductID as ProductID, Pro.ProductName AS Name, to_char(Pro.ProductPrice,'$99.99') AS Price
2 FROM Product_mys Pro
3 WHERE Pro.ProductPrice = (SELECT MAX(Pro.ProductPrice)
4 FROM OrderDetail_mys Pro);
WHERE Pro.ProductPrice = (SELECT MAX(Pro.ProductPrice)
*
ERROR at line 3:
ORA-00904: "PRO"."PRODUCTPRICE": invalid identifier
SQL>
SQL> --question 16
SQL>
SQL> SELECT Cat.CatID as Cat_ID,Pro.ProductID as Prod_ID, Pro.ProductName AS Prod_Name, to_char(Pro.ProductPrice,'$99.99') AS Price
2 FROM Category_mys Cat,Product_mys Pro
3 WHERE Pro.ProductPrice = (SELECT MIN(Pro.ProductPrice)
4 FROM OrderDetail_mys Pro);
WHERE Pro.ProductPrice = (SELECT MIN(Pro.ProductPrice)
*
These are the schema tables I'm using for the values
CREATE TABLE Dept_mys (
DeptID Number(3) NOT NULL,
DeptName VARCHAR(20) NOT NULL,
PRIMARY KEY (DeptID)
) ;
CREATE TABLE Commission_mys (
CommClass CHAR(1) NOT NULL,
CommRate Number(2,2) NOT NULL,
PRIMARY KEY (CommClass)
) ;
CREATE TABLE Category_mys (
CatID Number(3) NOT NULL,
catName VARCHAR(20) NOT NULL,
PRIMARY KEY (CatID)
) ;
CREATE TABLE SalesRep_mys (
SalesRepID NUMBER(4) NOT NULL,
SalesRepFName VARCHAR(20) NOT NULL,
SalesRepLName VARCHAR(20) NOT NULL,
DeptID NUMBER(3) NOT NULL,
CommClass CHAR(1) NOT NULL,
PRIMARY KEY (SalesRepID),
FOREIGN KEY (DeptID) REFERENCES Dept_mys,
FOREIGN KEY (CommClass) REFERENCES Commission_mys
) ;
CREATE TABLE Customer_mys (
CustID CHAR(5) NOT NULL,
CustFName VARCHAR(20) NOT NULL,
CustLName VARCHAR(20) NOT NULL,
CustPhone CHAR(10),
SalesRepID NUMBER(4) NOT NULL,
PRIMARY KEY (CustID),
FOREIGN KEY (SalesRepID) REFERENCES SalesRep_mys
) ;
CREATE TABLE Order_mys (
OrderID NUMBER(3) NOT NULL,
OrderDate DATE NOT NULL,
CustID CHAR(5) NOT NULL,
PRIMARY KEY (OrderID),
FOREIGN KEY (CustID) REFERENCES Customer_mys
) ;
CREATE TABLE Product_mys (
ProductID NUMBER(3) NOT NULL,
ProductName VARCHAR(30) NOT NULL,
CatID Number(3) NOT NULL,
PRIMARY KEY (ProductID),
FOREIGN KEY (CatID) REFERENCES Category_mys
) ;
CREATE TABLE OrderDetail_mys (
OrderID NUMBER(3) NOT NULL,
ProductID NUMBER(3) NOT NULL,
ProductQty NUMBER(4) NOT NULL,
ProductPrice NUMBER(6,2) NOT NULL,
PRIMARY KEY (OrderID, ProductID),
FOREIGN KEY (OrderID) REFERENCES Order_mys,
FOREIGN KEY (ProductID) REFERENCES Product_mys
) ;
You're attempting to use ProductPrice from Product_mys which doesn't have that column.
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;