How to join one to many tables in Oracle database - sql

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.

Related

Oracle - Count Function (Orders Sold By each employee)

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.

Why don't the values for a single studentid add up while i execute this SQL statement even though i used Distinct?

I have to write a query to display the studentid and the total fees paid by each student and then sort the result based on studentid in ascending order.
I have used Distinct so that studid is displayed only once but still I don't get the expected output.
My code:
select distinct s.studid,c.fees as total_fees from Student s join
Registration r on s.studid=r.studid join Course c on r.courseid=c.courseid
group by s.studid,c.fees order by s.studid;
My Output:
Expected Output:
You join registration.
That would give you one record per student and registration,
which is what you want.
Calling distinct on it then, though, means you only get one of those records.
What you want to do is sum up those fees, group the results per student, and order by studid.
SELECT s.studid, SUM(c.fees) as total_fees FROM student s
JOIN registration r ON s.studid = r.studid
JOIN course c ON r.courseid=c.courseid
GROUP BY s.studid
ORDER BY s.studid;
You are querying by distinct combination of the student ID and the fees instead of summing the fees:
SELECT s.studid, SUM(c.fees) as total_fees
FROM student s
JOIN registration r ON s.studid = r.studid
JOIN course c ON r.courseid=c.courseid
GROUP BY s.studid
ORDER BY s.studid ASC

Multiple unwanted records in Group by clause in Postgress

I have two table and I am joining them together then running a group by clause. The problem is that I keep getting unwanted data.
client table
----------
name
company_id
created_at
company table
-----------
name
Query:
SELECT company.name, clients.name, MIN (created_at) created_at
FROM company
INNER JOIN client
ON client.company_id = company.id
group by company.name, client.name
The query returns to me all the users, but what I want is only each one that was first created in each company. What should I change knowing that I need the clients names.
If you want the first one in each company, then use distinct on. This is a nice construct available only in Postgres:
SELECT DISTINCT ON (co.name) co.name, cl.name, cl.created_at
FROM company co INNER JOIN
client cl
ON cl.company_id = co.id
ORDER BY co.name, cl.created_at asc;

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

Three tables join given me the all combination of records

When i written the query like the following.. It's written the combination of all the records.
What's the mistake in the query?
SELECT ven.vendor_code, add.address1
FROM vendor ven INNER JOIN employee emp
ON ven.emp_fk = emp.id
INNER JOIN address add
ON add.emp_name = emp.emp_name;
Using inner join, you've to put all the links (relations) between two tables in the ON clause.
Assuming the relations are good, you may test the following queries to see if they really make the combination of all records:
SELECT count(*)
from vendor ven
inner join employee emp on ven.emp_fk = emp.id
inner join address add on add.emp_name = emp.emp_name;
SELECT count(*)
add.address1
from vendor ven, employee emp, address add
If both queries return the same result (which I doubt), you really have what you say.
If not, as I assume, maybe you are missing a relation or a restriction to filter the number of results.