SQL is my "weak" part, however I can handle it. I was wondering if there are better ways of writing my queries below. Any alternative like small tweaks or editing is welcome, it's a way for me to learn more.
I have the expected/correct results on all of them.
Shows which shops have the product code 1234 giving the code, name and address of them.
SELECT k.ShopCode, Name, Address FROM shops as k
INNER JOIN availability as d ON k.ShopCode=d.ShopCode
WHERE d.ProductCode = '1234';
Shows the average value of transactions in the shop with code 2, including the name, address and the average price of transactions.
SELECT Name, Address, AVG(QUANTITY*ProductPrice) AS AveragePrice
FROM shop AS k
INNER JOIN chashier AS t ON k.ShopCode = t.ShopCode
INNER JOIN buy AS a ON t.CashierCode = a.CashierCode
INNER JOIN contains AS p ON a.BuyCode = p.BuyCode
WHERE t.ShopCode = '1'
GROUP BY NAME, ADDRESS
Shwos the name, address and the amount of the top three in total purchasing customers of the chain.
SELECT TOP 3 BuyerName, BuyerSurname, Address, SUM(QUANTITY*ProductPrice) AS AveragePrice
FROM buyer AS pe
INNER JOIN buy AS a ON pe.CardCode = a.BuyerCode
INNER JOIN contains AS p ON a.BuyCode= p.BuyCode
GROUP BY pe.CardCode, BuyerName, BuyerSurname, Address
ORDER BY AveragePrice DESC;
This one shows the new products that not yet launched in the shops and therefore not participate in any buy. The answer includes the code and description of the product.
SELECT BuyCode, description from products
WHERE product.BuyCode not in
( select distinct store_has_products.product_id FROM store_has_products)
It shows the manager of each shop. The answer includew the store code and the name of the manager.
SELECT works.store_id as, employee.first_name, employee.last_name FROM manager
left join employee on manager.id = employee.id
lefti join works on employee.id = works. employee_id
I'd change the 4th query (products that have no launched yet) to use EXISTS...
SELECT BuyCode, description FROM products
WHERE NOT EXISTS (SELECT * FROM store_has_products WHERE product_id = products.BuyCode)
(I've seen very poor performance from using IN over large sets. That may have improved, I've not used MySQL in ages. But this is at least an alternative formulation.)
The rest look fine to me...
A 'hot' alternative is ORM (object relational mapping)
If you don't like sql try Object-relational mapping for PHP.
There's nothing wrong with your queries. Just a couple of picks: