Utilizing subqueries in sql. Displaying no output - sql

I have a task of retrieving the first and last names of customers where their orders have not shipped out. Below is the tables made to get this result ↓↓↓
CREATE TABLE employees
(
employee_id INT NOT NULL,
last_name VARCHAR(20) NOT NULL,
first_name VARCHAR(20) NOT NULL,
manager_id INT,
CONSTRAINT employees_pk PRIMARY KEY (employee_id),
CONSTRAINT emp_fk_mgr
FOREIGN KEY (manager_id) REFERENCES employees(employee_id)
);
CREATE TABLE orders
(
order_id INT NOT NULL,
customer_id INT NOT NULL,
order_date DATE NOT NULL,
shipped_date DATE,
employee_id INT,
CONSTRAINT orders_pk PRIMARY KEY (order_id),
CONSTRAINT orders_fk_customers
FOREIGN KEY (customer_id) REFERENCES customers (customer_id),
CONSTRAINT orders_fk_employees
FOREIGN KEY (employee_id) REFERENCES employees (employee_id)
);
CREATE TABLE order_details
(
order_id INT NOT NULL,
item_id INT NOT NULL,
order_qty INT NOT NULL,
CONSTRAINT order_details_pk PRIMARY KEY (order_id, item_id),
CONSTRAINT order_details_fk_orders
FOREIGN KEY (order_id) REFERENCES orders (order_id),
CONSTRAINT order_details_fk_items
FOREIGN KEY (item_id) REFERENCES items (item_id)
);
Currently, I have the following query made up, but it keeps generating No Output.
SELECT c.customer_first_name
FROM order_details od
JOIN orders o ON od.order_id = o.order_id
JOIN customers c ON o.customer_id = c.customer_id
WHERE c.customer_id IN (SELECT shipped_date FROM orders WHERE shipped_date IS NULL);
Can someone help me to get the result I need?

Presumably, your customers are not dates and you actually intend:
WHERE c.customer_id IN (SELECT o.customer_id FROM orders o WHERE shipped_date IS NULL)

Related

Getting ORA-00942 Error for "Table or View does not exist"

Not sure why, but referenced tabled not able to be identified. The following is the code:
CREATE TABLE Store (
Store_ID NUMBER NOT NULL,
Dept_ID NUMBER NOT NULL,
Manager_ID NUMBER NOT NULL,
Employee_ID NUMBER NOT NULL,
CONSTRAINT Store_PK PRIMARY KEY (Store_ID),
CONSTRAINT Manager_FK FOREIGN KEY (Manager_ID) REFERENCES Manager (Manager_ID),
CONSTRAINT Employee_FK FOREIGN KEY (Employee_ID) REFERENCES Employee (Employee_ID)
);
CREATE TABLE Manager
(
Manager_ID NUMBER NOT NULL,
Employee_ID NUMBER NOT NULL,
Manager_Lname VARCHAR(30) NOT NULL,
Manager_Fname VARCHAR(30) NOT NULL,
CONSTRAINT Manager_PK PRIMARY KEY (Manager_ID),
CONSTRAINT Employee_FK FOREIGN KEY (Employee_ID) REFERENCES Employee (Employee_ID)
);
CREATE TABLE OrderTable
(
Order_ID NUMBER NOT NULL,
Customer_ID NUMBER NOT NULL,
Vendor_ID NUMBER NOT NULL,
Product_ID NUMBER NOT NULL,
Tracking_Num NUMBER NOT NULL,
CONSTRAINT Order_PK PRIMARY KEY (Order_ID),
CONSTRAINT Customer_FK FOREIGN KEY (Customer_ID) REFERENCES Customer (Customer_ID),
CONSTRAINT Vendor_FK FOREIGN KEY (Vendor_ID) REFERENCES Vendor (Vendor_ID),
CONSTRAINT Product_FK FOREIGN KEY (Product_ID) REFERENCES Product (Product_ID),
CONSTRAINT Shipping_FK FOREIGN KEY (Tracking_Num) REFERENCES Shipping (Tracking_Num)
);
CREATE TABLE Customer
(
Customer_ID NUMBER NOT NULL,
Customer_Lname VARCHAR(30),
Customer_Fname VARCHAR(30),
Email VARCHAR(30),
Payment_Type VARCHAR(15),
CONSTRAINT Customer_PK PRIMARY KEY (Customer_ID)
);
CREATE TABLE Product
(
Product_ID NUMBER NOT NULL,
Price NUMBER NOT NULL,
Brand VARCHAR(30),
Classification VARCHAR(30),
CONSTRAINT Product_PK PRIMARY KEY (Product_ID)
);
CREATE TABLE Employee
(
Employee_ID NUMBER NOT NULL,
Title INT,
Employee_Lname VARCHAR(30),
Employee_Fname VARCHAR(30),
CONSTRAINT Employee_PK PRIMARY KEY (Employee_ID)
);
CREATE TABLE Vendor
(
Vendor_ID NUMBER NOT NULL,
Product_ID NUMBER NOT NULL,
Quantity NUMBER,
CONSTRAINT Vendor_PK PRIMARY KEY (Vendor_ID),
CONSTRAINT Product_FK FOREIGN KEY (Product_ID) REFERENCES Product (Product_ID)
);
CREATE TABLE Retail
(
Retail_ID NUMBER NOT NULL,
Product_ID NUMBER NOT NULL,
Vendor_ID NUMBER NOT NULL,
Order_ID NUMBER NOT NULL,
Price NUMBER NOT NULL,
Quantity NUMBER NOT NULL,
CONSTRAINT Retail_PK PRIMARY KEY (Retail_ID),
CONSTRAINT Product_FK FOREIGN KEY (Product_ID) REFERENCES Product (Product_ID),
CONSTRAINT Vendor_FK FOREIGN KEY (Vendor_ID) REFERENCES Vendor (Vendor_ID),
CONSTRAINT OrderTable_FK FOREIGN KEY (Order_ID) REFERENCES OrderTable (Order_ID)
);
CREATE TABLE Shipping
(
Tracking_Num NUMBER NOT NULL,
Order_ID NUMBER NOT NULL,
Vendor_ID NUMBER NOT NULL,
Address VARCHAR2(50),
Shipping_Date NUMBER NOT NULL,
CONSTRAINT Shipping_PK PRIMARY KEY (Tracking_Num),
CONSTRAINT OrderTable_FK FOREIGN KEY (Order_ID) REFERENCES OrderTable (Order_ID),
CONSTRAINT Vendor_FK FOREIGN KEY (Vendor_ID) REFERENCES Vendor (Vendor_ID)
);
Assuming this is the order you're creating your tables, this is likely your problem:
CREATE TABLE Store (
...
CONSTRAINT Manager_FK FOREIGN KEY (Manager_ID)
REFERENCES Manager (Manager_ID)
...
You're referencing a table in your table definition that doesn't exist yet.

How to consolidate my data for SQL Join Clause

I have to write a query with the following criteria:
Write a query to list the title and artist of ONLY the items that have been ordered. Only list each title once.
Here are the CREATE tables that were made that cannot be changed.
CREATE TABLE artists
(
artist_id INT NOT NULL,
artist_name VARCHAR(30),
CONSTRAINT artist_pk PRIMARY KEY (artist_id)
);
CREATE TABLE items
(
item_id INT NOT NULL,
title VARCHAR(50) NOT NULL,
artist_id INT NOT NULL,
unit_price DECIMAL(9,2) NOT NULL,
CONSTRAINT items_pk PRIMARY KEY (item_id),
CONSTRAINT items_fk_artists
FOREIGN KEY (artist_id) REFERENCES artists (artist_id)
);
CREATE TABLE orders
(
order_id INT NOT NULL,
customer_id INT NOT NULL,
order_date DATE NOT NULL,
shipped_date DATE,
employee_id INT,
CONSTRAINT orders_pk PRIMARY KEY (order_id),
CONSTRAINT orders_fk_customers
FOREIGN KEY (customer_id) REFERENCES customers (customer_id),
CONSTRAINT orders_fk_employees
FOREIGN KEY (employee_id) REFERENCES employees (employee_id)
);
Here is what I have done for my script.
SELECT
items.title, artists.artist_name, orders.order_date
FROM
items
JOIN
orders
JOIN
artists;
Please let me know how I can consolidate.
You should say the structures of tables with respect to keys between tables.

I can't create table error ORA_00903

DROP TABLE Orders CASCADE CONSTRAINTS;
DROP TABLE Order_Items CASCADE CONSTRAINTS;
CREATE TABLE Orders (
ORDER_NO VARCHAR(5),
ORDER_DATE DATE CONSTRAINT BNL_ORDER_DATE_NN NOT NULL,
CUSTOMER_NAME VARCHAR(20) CONSTRAINT BNL_CUSTOMER_NAME_NN NOT NULL,
POSTAGE NUMBER(5,2) CONSTRAINT BNL_POSTAGE_NN NOT NULL,
TOTAL NUMBER(10,2) CONSTRAINT BNL_TOTAL_NN NOT NULL,
CONSTRAINT ORD_ID_PK PRIMARY KEY (ORDER_NO)
);
CREATE TABLE Order_ITEMS (
ITEM_NO VARCHAR(10),
ITEM_DES VARCHAR(20),
IETM_SIZE VARCHAR(5) CONSTRAINT ITE_ITEM_SIZE_NN NOT NULL,
ITEM_COST NUMBER(10,2) CONSTRAINT ITE_ITEM_COST_NN NOT NULL,
QTY NUMBER(5) CONSTRAINT ITE_QTY_NN NOT NULL,
ORDER_NO NUMBER(5),
CONSTRAINT ITE_ID_PK PRIMARY KEY (ITEM_NO),
CONSTRAINT ITE_ORD_FK FOREIGN KEY(ORDER_NO)
REFERENCES ORDER(ORDER_NO)
);
The Orders table is working but the Order_Items table is not working show error ORA_00903. I had change many other names, but it still show the error ORA-00903: invalid table name.
Your foreign key constraint is incorrect:
CONSTRAINT ITE_ORD_FK FOREIGN KEY(ORDER_NO)
REFERENCES ORDER(ORDER_NO)
^
You have named the referenced table ORDER instead of ORDERS.

SQL invalid identifier error.....?

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.

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;