R
R
Ruben Chobanyan2017-12-19 18:21:02
MySQL
Ruben Chobanyan, 2017-12-19 18:21:02

Write 3 sql queries to two tables?

Good afternoon, please clarify how to write three queries (I wrote the first query, but I'm not sure if it's correct) to two tables
There is a table of manufacturers
MF_ID,
MF_NAME
and a table of products
PD_ID,
PD_NAME, (product name)
PD_MFID (manufacturer id)
PD_PRICE
1. It is necessary to select those manufacturers that do not have any entries in the product table

SELECT t1.MF_ID
FROM производители t1
WHERE NOT EXISTS (SELECT t2.PD_MFID FROM продукты t2 WHERE t1.MF_ID = t2.PD_MFID)

2. Select the manufacturer of the product with the highest price
3. Select the 3rd manufacturer in the number of different product names.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Polyakov, 2017-12-19
@rachobanyan

1) SELECT t1.MF_ID, count(*)
FROM `producers` t1
LEFT JOIN `products` t2 ON t1.MF_ID = t2.PD_MFID
GROUP BY t1.MF_ID
HAVING count(*)=0
2) SELECT t1.MF_ID, max (t2.PD_PRICE)
FROM `manufacturers` t1
LEFT JOIN `products` t2 ON t1.MF_ID = t2.PD_MFID
GROUP BY t1.MF_ID
ORDER BY max(t2.PD_PRICE) DESC
3) SELECT t1.MF_ID, count(*)
FROM `producers` t1
LEFT JOIN `products` t2 ON t1.MF_ID = t2.PD_MFID
GROUP BY t1.MF_ID
ORDER BY count(*) DESC
LIMIT 2,1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question