Selecting data using three tables only joining two tables - sql

I am trying to select data using three tables. I need to get an equity number and contract date from an actor and contract table where the name of the film = x from a film table.
I have done:
SELECT equity_number, contract_date
FROM actor,
contract,
film
WHERE actor.equity_number = contract.equity_number
and title = 'x'
Although I am getting a column ambiguously defined error.

You don't have any Relationship with the film table.
And use a modern way of writing sql :)
SELECT a.equity_number, c.contract_date
FROM actor a
INNER JOIN contract c on a.equity_number = c.equity_number
INNER JOIN film f on f.SOMERELATIONSHIPID = c.SOMERELATIONSHIPID
WHERE f.title = 'x'

I suggest you to use JOINS instead of old comma-separated syntax. After It set aliases for each table and use these aliases in select list. Something like:
SELECT a.equity_number,
c.contract_date
FROM actor a
LEFT JOIN contract c ON a.equity_number = c.equity_number
LEFT JOIN film f ON a.film_id = f.id -- there should be related columns
AND f.title = 'x'

Related

How to count total crossover from two tables each with specific conditions

I am working from two tables in a dataset. Let's call the first one 'Demographic_Info', the other 'Study_Info'. The two tables both have a Subject_ID column. How can I run a query that will return all of the Subject_IDs where Sex = Male (from Demographic_Info) but also where the Study Case = Case (from Study_Info)?
Is this an inner join? Do I need to make a combined table?
I just don't know what function to use. I know how to select for each of these conditions in each table individually, but not how to run them against eachother.
Yes, you will want to inner join and then use the where clause to filter on both tables.
select
s.Subject_ID
from `Study_info` s
inner join `Demographic_info` d on s.Subject_ID = d.Subject_ID
where d.Sex = 'Male'
and s.Study_Case = 'Case' -- Unclear from your question about the actual field name
The aliases s and d will be useful for organizing which table each field comes from (or if the same field occurs in both tables).
Similarly, you could filter first and then perform the join.
with study as (select * from `Study_info` where Study_Case = 'Case'),
demographics as (select * from `Demographic_info` where Sex = 'Male')
select s.Subject_ID
from study s
inner join demographics d on s.Subject_ID = d.Subject_ID

3 tables to join

I am stuck in my liaison of my 3 tables.
I have a table which is candidates with 3 fields (id_candidate, name_candidate, firstname_candidate)
An other table nammed lessons with 5 fields (id_lesson, price_lesson, date_lesson, fk_candidate, fk_monitor)
I have a table which is monitors with 3 fields (id_monitor, name_monitor, firstname_monitor)
I can join 2 tables (candidates and lessons)
Here is the request
SELECT *
FROM lessons INNER JOIN
candidates
ON lessons.fk_candidate=candidates.id_candidate
ORDER BY id_candidate ASC
But my problem is that. I don't understand how to join 3 tables?
In fact, the name of monitor must appear on the table lessons.
I have tried that
SELECT *
FROM candidates id_candidate INNER JOIN
lessons id_lesson
ON lessons.fk_candidate = candidates.id_candidate INNER JOIN
monitors id_monitor
ON lessons.fk_monitor = monitors.id_monitor;
Presumably, something like this:
SELECT c.*, l.*, m.name_monitor -- list out the columns you want explicitly
FROM candidates c INNER JOIN
lessons l
ON l.fk_candidate = c.id_candidate INNER JOIN
monitors m
ON l.fk_monitor = m.id_monitor;
Notes:
Using table aliases is a good idea. I recommend abbreviations for the table name.
After you have defined the table alias, use them. Your on clauses still referred to the table name.
Explicitly list the columns that you want.

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;

SQL get data from multipe tables

I would like to return the follow question in SQL.
In which action movie play more female then male?
I got a table which movie_id/title/genre, a table with cast_id/gender.
And between this table a link table with movie_id and cast_id.
Can someone explain how to extract the data from a construction like this?
I came up with this so far
SELECT titel, geslacht, genre
FROM imdb.film
INNER JOIN imdb.cast ON cast.cast_id = film.film_id
You can join the three tables involved and group by title to check for the condition.
SELECT f.title
FROM imdb.film f
INNER JOIN imdb.cast c ON c.cast_id=f.film_id
INNER JOIN link_table l ON l.movie_id=f.movie_id and l.cast_id=c.cast_id
WHERE f.genre = 'ACTION'
GROUP BY f.title
HAVING SUM(c.gender = 'F') > SUM(c.gender = 'M')
Replace link_table in the query with the actual table name.

SQL Server query issue - ambiguous column

I have four tables :
Applicant (aid, aname)
entrance_test (Etid, etname)
etest_centre (etcid, location)
etest_details (aid, etid, etcid, etest_dt)
I want to select the number of applicants who have appeared for each test, test center wise.
This is my current query:
select
location, etname, count(Aid) as number of applicants
from
applicant as a
inner join
etest_details as d on a.aid = d.aid
inner join
Entrance_Test as t on t.Etid = d.Etid
inner join
Etest_Centre as c on c.Etcid = d.Etcid
group by
Location, Etname
This is the error I am getting :
Ambiguous column name 'Aid'
You have the column aid in multiple tables, and it doesn't know which to pick from. You should specify which table it is from using the aliases you defined.
In this case, since a.Aid is the same as d.Aid (due to the JOIN), I'm using the a alias, but do keep in mind if location and etname also appear in multiple tables, you need to specify which table it should pick from.
Select c.location, t.etname, Count(a.Aid)
From Applicant As a
Inner Join etest_details As d On a.aid = d.aid
Inner Join Entrance_Test As t On t.Etid = d.Etid
Inner Join Etest_Centre As c On c.Etcid = d.Etcid
Group By c.Location, t.Etname
As a rule of thumb, when you have multiple sources in one query, you should always be explicit about which table it should come from. Even if you're sure it only exists in one of them, it's a good habit to get into to avoid issues like this in the future.
You need to mention the alias in the COUNT clause. Since you are using aliases, it would be better if you use them in the SELECT and GROUP BY sections as well. In this case, it should be :
SELECT a.location,
a.etname,
COUNT(d.Aid)
FROM applicant AS a
INNER JOIN etest_details AS d ON a.aid = d.aid
INNER JOIN Entrance_Test AS t ON t.Etid = d.Etid
INNER JOIN Etest_Centre AS c ON c.Etcid = d.Etcid
GROUP BY a.Location,
a.Etname