my sql query is not working according - sql

sql structure
SELECT
opreator.o_name,
city.c_name as dept,
city.c_name as destination,route.fare
FROM opreator
INNER JOIN route ON opreator.id=route.id
LEFT OUTER JOIN city ON city.id=route.dep
i want to print data as
o_name| dep| dest|fare

You should use two join on city using table alias
SELECT
opreator.o_name,
c1.c_name as dep,
c2.c_name as destination,
route.fare
FROM route
INNER JOIN opeator on opreator.id=route.id
INNER JOIN city as c1 on c1.id = route.dep
INNER JOIN city as c2 on c2.id = route.dest

When you JOIN two tables together the condition should be something like WHERE table1.foreign_key = table2.primary_key.
Your first join has a condition WHERE table1.primary_key = table2.primary_key which doesn't make sense. The primary key for one table has nothing to do with the primary key of the other table.
You also need to join to the city table twice, once for dep and again for dest.
Try this:
SELECT opreator.o_name, c1.c_name as dept, c2.c_name as destination, route.fare
FROM route
JOIN opreator ON route.opreator = opreator.id
JOIN city c1 ON route.dep = c1.id
JOIN city c2 ON route.dest = c2.id

Related

self join after an inner join

I am finding what cities have the same name in different states. The city name and state name are in seperate tables (cities and states) and can be inner joined over a seperate common column.
select c1.city, c1.state, c2.city, c2.state
from cities
inner join states on cities.commonid = states.commonid
After inner joining i need to self join to perform a function as follows
select c1.city, c1.state, c2.city, c2.state
from *joined table* c1 join
*joined table* c2
on c1.city = c2.city and c1.state <> c2.state
i am wondering how i can self join a table that is the result of another join in the same query
output will be like this
+----------+-------+--------+--------+
| city1 | state1|city2 |state2 |
+----------+-------+--------+--------+
| x | melb | x | syd |
| y | bris | y | ACT |
+----------+-------+--------+--------+
I assume that the table cities has a column like state_id that references a column state_id in the table states (change the names to the actual names of the columns).
First do a self join for cities with the conditions:
c1.city = c2.city AND c1.state_id < c2.state_id
The < operator makes sure that each pair of cities wil be returned only once.
Then join 2 copies of states, because each of them will be used to get the name of the state for each of the 2 cities:
SELECT c1.city city1, s1.state state1,
c2.city city2, s2.state state2
FROM cities c1
INNER JOIN cities c2 ON c1.city = c2.city AND c1.state_id < c2.state_id
INNER JOIN states s1 ON s1.state_id = c1.state_id
INNER JOIN states s2 ON s2.state_id = c2.state_id
ORDER BY city1
You can do select of your inner join query and give it alias.
Then it will become something like below...
Select c.city,c.state from
(Select City,state from cities inner join states where cities.id = states.id) as c
Now make a self join for c.
I would perform a pre-query of all cities that have more than one state. Then join to the states table to see which states they are encountered.
select
Duplicates.city,
s.state
from
( select c1.city
from cities c1
group by c1.city
having count(*) > 1 ) Duplicates
JOIN cities c2
on Duplicates.city = c2.city
JOIN states s
on c2.commonid = s.commonid
order by
Duplicates.city,
s.state
By NOT trying to do cross-tab of just two city/states, you would get a single list. If one city name exists in 5 states, how would you plan on showing that. This way you would see all alphabetized.
I would suggest a CTE:
with cs as (
select c.name as city_name, s.name as state_name
from cities c join
states s
on c.commonid = s.commonid
)
select cs1.*, cs2.*
from cs cs1 join
cs cs2
on cs1.name = cs2.name and cs1.state <> cs2.state;

LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join

I have the following structure for table categories:
And I'm trying to do a self join on parentId = id to get the parent category but I also want the query to include the row without the join.
I tried doing:
select *
from `project.dataset.categories` c
left join `project.dataset.categories` p1 on p1.id = c.parentId or p1.id = c.id
But this throws
LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join.
For reasons I can't understand.
So basically I'm expecting something like this in the end:
Sadly using union is not an option because this is just a simplification of a much more complex query.
Any help appritiated.
I think you can do:
select cc.*
from `project.dataset.categories` c cross join
unnest(array[id, parent_id]) c_id left join
categories cc
on cc.id = c_id
-- where c.id = 2106 -- you seem to want this condition as well
This creates an array with the two columns you want to join -- and then unnests them into two separate rows for the join.
Or just use or and exists:
select c.*
from `project.dataset.categories` c
where c.id = 2106 or
exists (select 1
from `project.dataset.categories` c2
where c2.id = 2016 and c2.parentid = c.id
);

Nested 'Where'?

I have a table named Actor, with only a column for City (CityId). I want to return the number of actors in a particular State (StateId). The catch however is that I have separate tables for City, County, and finally State (City has CountyId, County has StateId). How do I this in a T-SQL query?
I have a solution that involves nested Select statements, something like:
SELECT COUNT(1)
FROM Actor a
WHERE a.CityId IN
(SELECT CityId FROM City WHERE CountyId IN...)
...but is there a more efficient way to do this? Thanks
You can use this query to get your output
----------------------------------------------------------
SELECT COUNT(ActorId)
FROM Actor a
INNER JOIN City c ON a.cityId = c.cityId
INNER JOIN Country con ON c.countryId = con.countryId
INNER JOIN STATE s ON con.stateId = s.stateId
GROUP BY s.stateId
Use JOINS to query your data.
I am using INNER JOIN here.
Assuming that you have CountryId in your City Table, You can do it following way.
In case you don't have countryId in your City Table you have to apply one more INNER JOIN on State Table.
SELECT COUNT(1) FROM Actor a INNER JOIN
City b ON a.CityId = b.CityId
WHERE b.CountryId IN (...)
You can easily put the JOINS across different table that you have and then use the Group By clause to find out the total number of actors from specific state.
I have used the column name on the basis of my wild guess, you can change them with the original name that you have in your database.
SELECT StateId,
Count(ActorId) AS Total
FROM ACTOR
INNER JOIN City ON Actor.CityId = City.CityId
INNER JOIN County ON County.CountyId = City.CountyId
INNER JOIN State ON State.StateId = County.StateId
GROUP BY State.StateId
Assuming the relation names, you can do something like this with joins:
select s.ID, s.Name, count(*)
from Actors a
inner join Cities c on c.ID = a.CityID
inner join County cn on cn.ID = c.CountyID
inner join State s on s.ID = cn.StateID
group by s.ID, s.Name
If you only need the StateId you don't even need to join with states, this will do:
select cn.StateID, count(*)
from Actors a
inner join Cities c on c.ID = a.CityID
inner join County cn on cn.ID = c.CountyID
group by cn.StateID

Two Inner Joins MYSQL

How would I preform two inner joins in one query?
Ie: three tables
Invoice
Address
Client
Invoice has a column which references an id in clients. It also has a column which references an address. I need to get both the clients name from the matched table and the address from the matched table. How would I INNER JOIN both tables?
I'll add a few details...
invoice has rows address(references address id), client(references client id), id and notes
client has rows first_name, last_name
address has rows street_name and city
I need to pull up
You can have as many JOIN clauses as you need in the query. Each has an ON clause where you specify the related columns between the joined tables.
SELECT
columns
FROM
invoice
INNER JOIN
address
ON
join_condition
INNER JOIN
client
ON
join_condition
Something like:
SELECT
c.*, i.*, a.*
FROM
invoices i
INNER JOIN
client c
ON
i.clientid = c.clientid
INNER JOIN
address a
ON
a.clientid = c.clientid
WHERE
i.id = 21
Don't forget you only select the fields you require, not * (all).
A sample e.g. based on learning from #Dan Grossman above:
SELECT FILM.TITLE, ACTOR.FIRST_NAME, ACTOR.LAST_NAME FROM ACTOR
INNER JOIN FILM_ACTOR
ON ACTOR.ACTOR_ID = FILM_ACTOR.ACTOR_ID
INNER JOIN FILM
ON FILM_ACTOR.FILM_ID = FILM.FILM_ID
WHERE ACTOR.FIRST_NAME = 'Nick' AND ACTOR.LAST_NAME = 'Wahlberg'

Left Join two columns with the same type of data, different values

In my table I have two columns, cars.station1_id and cars.station2_id. These columns contain an ID number referencing stations.id.
I need to take the two ID's and retrieve stations.name for each of them.
How do I do this?
This is my code to join one column:
SELECT station1_id, station2_id FROM cars
LEFT JOIN stations
ON stations.id = cars.station_id
SELECT a.name, b.name
FROM cars
LEFT JOIN stations a ON a.id = station_id
LEFT JOIN stations b ON b.id = station2_id
The key is to use a different alias each time.
s1 and s2 in this case:
SELECT station1_id, station2_id
FROM cars
LEFT JOIN stations s1
ON s1.id = cars.station1_id
LEFT JOIN stations s2
ON s1.id = cars.station2_id