Oracle - Count Function (Orders Sold By each employee) - sql

Having a problem figuring out how to display results that show 2 columns; one being the employee id and the second being the orders sold by that employee.
The 2 tables in question are Employees and Orders. The primary keys in each are called EmployeeID and OrderID. Orders has the foreign key EmployeeID in it. I am assuming I need to use the COUNT function to figure out how many orders there are in total, but the JOIN and finding out how to tie in the employee id with that has lost me.
All I have so far:
SELECT e.employeeid, COUNT(Orders) AS 'Count'
FROM Employees e
JOIN Orders o ON e.Employeeid = o.Employeeid
GROUP BY e.employeeid;

If there is a foreign key in the orders table why even look at employees unless you need something specific from Employees. I would just do something like this.
select employeeid, count(*) from Orders group by employeeid;

Your query is good, the only thing is you cannot count a table, so change COUNT(Orders) to COUNT(o.*) or COUNT(o.OrderID):
SELECT e.employeeid, COUNT(o.OrderID) AS OrderCount
FROM Employees e
JOIN Orders o ON e.Employeeid = o.Employeeid
GROUP BY e.employeeid;
Using a keyword like COUNT as identifier is not recommended, so I change it to OrcerCount, but it will work with 'Count' if you really wanted that.
This query will only give you the employees who have orders (at least one order). If you want to include employees who don't have any order, change JOIN to LEFT JOIN.

Related

SQL query that may be a subquery question or an inner join

I have a homework assignment that asks:
"For each customer whose total sales amount is greater than $250, list customer ID, customer name, and total sales amount of the customer. Give descriptive names for all computed fields."
Normally, I would never come to S.O. for homework answers, as I would just push through and figure it out, but this one seems to have me stuck.
This is my incorrect query to the question:
SELECT customerid
FROM customer
WHERE customerid In (SELECT sum(untipricesold) AS totalSales
FROM orderline WHERE sum(orderline.unitpricesold) > 250)
GROUP by customerid;
The result I got is:
"group function is not allowed here".
I have attached a screenshot of the relevant tables that SHOULD be used for this question. Again, this isn't something I would usually do and am completely against cheating for school, but please help.
You need HAVING clause when you are come with filtration after aggregation or groping :
So, i would inclined with JOIN :
SELECT c.customerid , c.customername, o.TotalSales
FROM customer c INNER JOIN
(SELECT customerid, SUM(untipricesold) AS TotalSales
FROM orderline O
GROUP by customerid
HAVING SUM(untipricesold) > 250
) o
ON o.customerid = c.customerid;
You need a HAVING clause. Also, you probably want to be selecting the customerid in the subquery, rather than the sum, since you are comparing to customerid in the outer query.
SELECT customerid
FROM customer
WHERE customerid IN (SELECT customerid FROM orderline
GROUP by customerid HAVING SUM(untipricesold) > 250);

Count the number of employees for every country

I have this task:
Count the number of employees for every country. Show only those countries, when works more than 20 employees
employee_id is dedicated for Employees table
country belongs to different table - Countries table and we need country_name from this table
I have no idea how to solve this task. Below what I was able to create. I think we should use Inner Join.
SELECT a.employee_id
, b.country_name
, COUNT(a.employee_id) AS count
FROM employees a
INNER JOIN countries b ON a.employee_id = b.country_name
GROUP BY b.country_name
WHERE employee_id >20;
I think I need help from the beginning.
Thanks
Your join doesn't seem correct but as I don't know the table structure, I can't say what the right column is (I'm going to assume that it should be country_name. Even so, try this:
SELECT b.country_name
, COUNT(a.employee_id) AS count
FROM employees a
INNER JOIN countries b ON a.country_name = b.country_name
GROUP BY b.country_name
HAVING COUNT(employee_id) >20;
When grouping you need to use the HAVING statement to filter.

How to join one to many tables in Oracle database

I have four tables:
Order, Employee, Supply, Supply_company
Order
------------------
Order_id
Order name
Emp_id
Employee
------------------
Emp_id
Emp_name
Supply
--------------------
Supply_id
Order_id
SupplierName
Supply_company
----------------------
Supply_company_id
Supply_id
Supplier_desc
address
In these 4 tables one employee has more than one order and one order has many supply ID's and for that one supply ID we have one supplier desc. I wanted to display Supplier_desc based on Emp_id. I am getting all the descriptions associated with all orders but I need to get specific desc for specific order, I have tried distinct, listagg, inner join and left outer join and used subquery in where clause but I didn't find any solution.
SELECT
O.EMP_ID,SC.*
FROM
SUPPLY_COMPANY SC
INNER JOIN
SUPPLY S
ON S.Supply_id=SC.Supply_id
INNER JOIN
Order O
ON O.Order_id=S.Order_id
WHERE O.Emp_id=123
Assuming that you have a particular EMP_ID and an ORDER_ID the following query will get you the EMP_NAME and all SUPPLIER_DESCs associated with that employee and order:
SELECT DISTINCT e.EMP_ID, e.EMP_NAME, s.SUPPLIER_DESC
FROM EMPLOYEE e
INNER JOIN ORDER o
ON o.EMP_ID = e.EMP_ID
INNER JOIN SUPPLY s
ON s.ORDER_ID = o.ORDER_ID
INNER JOIN SUPPLY_COMPANY c
ON c.SUPPLY_ID = s.SUPPLY_ID
WHERE e.EMP_ID = your_emp_id AND
o.ORDER_ID = your_order_id
Here we just walk down from EMPLOYEE, through ORDER and SUPPLY, to SUPPLY_COMPANY where the SUPPLIER_DESC can be found, then use the WHERE clause to filter the results down to the particular employee and order we care about - and since you probably don't want repeated rows we put a DISTINCT on the SELECT criteria to tell us we only want a single example of each unique combination. Just replace your_emp_id and your_order_id with the EMP_ID and ORDER_ID values you're interested in. Note that if you supply and EMP_ID and ORDER_ID which do not have anything in common on the EMPLOYEE and ORDER tables you'll get nothing returned by this query.
Best of luck.

SQL List the representative that handles the most customers

I have 2 tables SALESREP and CUSTOMER
I need to find out which salesrep has most customers
I have the following code:
select rep_lname, count(cust_num)
from customer inner join salesrep
on customer.REP_NUM = SALESREP.REP_NUM
group by rep_lname
This gives me all the rows with the number of customers each salesrep has, instead I need only one row that has the most customers.
How can I find the row with MAX num of customers?
select rep_lname, count(cust_num)
from customer inner join salesrep
on customer.REP_NUM = SALESREP.REP_NUM
group by rep_lname order by count(cust_num) desc limit 1;
I'm sure there's another way using having, but I can't seem to figure it out at the moment. Perhaps somebody else will chime in with it?
SELECT TOP 1 WITH TIES rep_lname, COUNT(cust_num)
FROM customer inner join salesrep
ON customer.REP_NUM = SALESREP.REP_NUM
GROUP BY rep_lname
ORDER BY count(cust_num) DESC

SQL aggregate functions and Group By

Employee
empId
empName
empStoreNum
Invoice
invNo
invAmount
empId
I have two tables above Employee and Invoice. I would like to setup a query to retrieve employee names, employee store numbers, and the total sales for each employee. I have issue a query below and it works but I was not able to retrieve employee store number.
SELECT Emp.empName, Sum(Inv.invAmount) AS totalSales
FROM Invoice AS Inv INNER JOIN Employee AS Emp ON Inv.empId = Emp.empId
GROUP BY Emp.empName
If I add Emp.empStoreNum to the SELECT I get the following error: “You tried to execute a query that does not include the specified expression ‘empStoreNum’ as part of an aggregate function.” How can modify the query to get employee store number also?
All non-aggregate columns in the select-list must be listed in the GROUP BY clause (unless you're using MySQL, which plays by a very different set of rules, or unless you are using a sufficiently recent version of PostgreSQL, which is able to deduce functional dependencies).
SELECT Emp.empName, Emp.empStoreNum, Sum(Inv.invAmount) AS totalSales
FROM Invoice AS Inv INNER JOIN Employee AS Emp ON Inv.empId = Emp.empId
GROUP BY Emp.empName, Emp.empStoreNum
Try adding empStoreNum to GROUP BY
SELECT Emp.empName, Emp.empStoreNum, Sum(Inv.invAmount) AS totalSales
FROM Invoice AS Inv INNER JOIN Employee AS Emp ON Inv.empId = Emp.empId
GROUP BY Emp.empName, Emp.empStoreNum
Add that second column to your group by