How do I add values from another table into a SQL response? - sql

I have the following tables:
games:
standings:
I am looking to build a table in the following format for each entry in the games table:
visitor_team_standing | home_team_standing | diff
I have been digging into joins to try and learn but still keep running into a problem.
SELECT
standings.standing visitor_standings,
games.diff
FROM games
INNER JOIN standings ON
games.visitor_team_id = standings.team_id;
However, I can't figure out how to also include the standing for home_team (home_team_id = team-_id).

You just join the standings table twice by giving each one a different alias.
SELECT
visitor.standings visitor_standings,
home.standings home_standings,
games.diff
FROM games
INNER JOIN standings as visitor ON
games.visitor_team_id = visitor.team_id
INNER JOIN standings as home ON
games.home_team_id = home.team_id;

You can try using this code as well.
SELECT
visitor.standings visitor_standings,
home.standings home_standings,
games.diff
FROM games
INNER JOIN standings as visitor
ON
games.visitor_team_id = visitor.team_id
INNER JOIN standings as Home
ON
games.home_team_id = home.team_id;

Related

Join tables when 3 column in first table that can point to same column in second table

I have the following DB structure:
And right now I can't make up a query to get
a creator data, admin data and tech data from item_contacts...
What kind of JOIN I need to use and how?
I think you want 3 joins on item_contacts - one for each column whose data you want to recover:
select
i.*,
cc.data as creator_data,
ca.data as admin_data,
ct.data as tech_data
from items i
inner join item_contacts cc on cc.contact_id = i.creator_id
inner join item_contacts ca on ca.contact_id = i.admin_id
inner join item_contacts ct on ct.contact_id = i.tech_id

How to SELECT data that connect in many to many relationship table

I want to select all components in material table and the equipment_name in equipment table.
The equipment_name is connected with the id_material and id_equipment. 1 material can consist of many equipment name and so the equipment.
I tried to use this code in MS Access query but it said Syntax Error (missing operand).
SELECT material.id_material, material.part_number_material, material.material_description,material.brand, material.stock, material.um, equipment.equipment_name, material.type, material.location, material.remarks FROM equipment_list a INNER JOIN material b ON a.PKid_material = b.id_material INNER JOIN equipment c ON a.PKid_equipment = c.id_equipment;
Example Data
I am a beginner.
If you're using Access, then you need parentheses for a three way join:
SELECT
m.id_material,
m.part_number_material,
m.material_description,
m.brand,
m.stock,
m.um,
e.equipment_name,
m.type,
m.location,
m.remarks
FROM
(equipment_list el INNER JOIN material m
ON el.PKid_material = m.id_material)
INNER JOIN equipment e
ON el.PKid_equipment = e.id_equipment;

Trouble understanding an SQL query for a many-to-many table

I have a many-to-many table which manages stock between stores and products
I'm trying to create a simple query to show all the rows in store_stock but in a more user friendly way by showing the product name and store name.
I've tried a few variations of this but I just keep getting errors
SELECT product.product_name, store.store_name, store_stock.quantity
FROM store_stock
INNER JOIN product ON store_stock.product_id = product.product.id
INNER JOIN store ON store_stock.store_id = store.store_id
When I do it via phpMyAdmin query tab it runs this: (However I had to change it from LEFT JOINs to INNER JOINs)
SELECT `product`.`name` AS `product_name`, `store`.`name` AS `store_name`, `store_stock`.`quantity`
FROM `product`
INNER JOIN `store_stock` ON `store_stock`.`product_id` = `product`.`product_id`
INNER JOIN `store` ON `store_stock`.`store_id` = `store`.`store_id`
ORDER BY `product`.`name` ASC, `store`.`name` ASC
This works, but what I don't understand is why it runs 'FROM product', instead of 'FROM store_stock' table

Select Query from 3 tables with foreign keys

I Have 3 Tables with foreign keys to each other.
I want to write a SQL Server Stored Procedure to select records from one of them.
Now, let's suppose that i want all the Winner records referring to the Player records referring to The Game with the ID=2, how can i proceed?
Thank you.
you have specified all the Winner records So that i have used the left join for player and game. But the Overall code works according to the where condition.
Try This,
select w.* from Winner w
left Join Player p on p.ID_player = w.player_FK
left join Game g on g.ID_game = p.Game_FK
where Game.ID_game = 2
You need to use a SELECT and INNER JOIN then to filter on GameID 2 you can use a WHERE clause.
SELECT ID_Winner, Name, Lastname, Player_FK
FROM Winner
INNER JOIN Player on Player.ID_Pplayer = Winner.Player_FK
INNER JOIN Game ON Game.ID_game = Player.Game_FK
WHERE Game.ID_game = 2

Order by join column but use distinct on another

I'm building a system in which there are the following tables:
Song
Broadcast
Station
Follow
User
A user follows stations, which have songs on them through broadcasts.
I'm building a "feed" of songs for a user based on the stations they follow.
Here's the query:
SELECT DISTINCT ON ("broadcasts"."created_at", "songs"."id") songs.*
FROM "songs"
INNER JOIN "broadcasts" ON "songs"."shared_id" = "broadcasts"."song_id"
INNER JOIN "stations" ON "broadcasts"."station_id" = "stations"."id"
INNER JOIN "follows" ON "stations"."id" = "follows"."station_id"
WHERE "follows"."user_id" = 2
ORDER BY broadcasts.created_at desc
LIMIT 18
Note: shared_id is the same as id.
As you can see I'm getting duplicate results, which I don't want. I found out from a previous question that this was due to selecting distinct on broadcasts.created_at.
My question is: How do I modify this query so it will return only unique songs based on their id but still order by broadcasts.created_at?
Try this solution:
SELECT a.maxcreated, b.*
FROM
(
SELECT bb.song_id, MAX(bb.created_at) AS maxcreated
FROM follows aa
INNER JOIN broadcasts bb ON aa.station_id = bb.station_id
WHERE aa.user_id = 2
GROUP BY bb.song_id
) a
INNER JOIN songs b ON a.song_id = b.id
ORDER BY a.maxcreated DESC
LIMIT 18
The FROM subselect retrieves distinct song_ids that are broadcasted by all stations the user follows; it also gets the latest broadcast date associated with each song. We have to encase this in a subquery because we have to GROUP BY on the columns we're selecting from, and we only want the unique song_id and the maxdate regardless of the station.
We then join that result in the outer query to the songs table to get the song information associated with each unique song_id
You can use Common Table Expressions (CTE) if you want a cleaner query (nested queries make things harder to read)
I would look like this:
WITH a as (
SELECT bb.song_id, MAX(bb.created_at) AS maxcreated
FROM follows aa
INNER JOIN broadcasts bb ON aa.station_id = bb.station_id
INNER JOIN songs cc ON bb.song_id = cc.shared_id
WHERE aa.user_id = 2
GROUP BY bb.song_id
)
SELECT
a.maxcreated,
b.*
FROM a INNER JOIN
songs b ON a.song_id = b.id
ORDER BY
a.maxcreated DESC
LIMIT 18
Using a CTE offers the advantages of improved readability and ease in maintenance of complex queries. The query can be divided into separate, simple, logical building blocks. These simple blocks can then be used to build more complex, interim CTEs until the final result set is generated.
Try by adding GROUP BY Songs.id
I had a very similar query I was doing between listens, tracks and albums and it took me a long while to figure it out (hours).
If you use a GROUP_BY songs.id, you can get it to work by ordering by MAX(broadcasts.created_at) DESC.
Here's what the full SQL looks like:
SELECT songs.* FROM "songs"
INNER JOIN "broadcasts" ON "songs"."shared_id" = "broadcasts"."song_id"
INNER JOIN "stations" ON "broadcasts"."station_id" = "stations"."id"
INNER JOIN "follows" ON "stations"."id" = "follows"."station_id"
WHERE "follows"."user_id" = 2
GROUP BY songs.id
ORDER BY MAX(broadcasts.created_at) desc
LIMIT 18;