Sql join in two tables and return empty tab as Unavaliable - sql

In a SQL join, table 1 contains person info with city and table 2 contains city matched to country such as:-
Table #1
ID Name City
-------------------------
1 Kishan Pokhara
2 Ram Delhi
3 Shyam Beijing
Table #2
City Country
----------------------
Pokhara Nepal
Delhi India
I want to get the person ID, Name, Country so while joining the tables I want these items and if there is no country available for a city, I want "Unavailable" written in the country columns. Thanks

Try the below using left join and use coalesce() function to replace null country as 'Unavailable'
select id, name, a.city,coalesce(country,'Unavailable') as country
from table1 a left join table2 b on a.city=b.city

Related

How to replace multiple columns in one SQL query?

I have 2 tables: country and trip.
A trip can have up to 3 country codes.
country table
country_code
country_name
FRA
FRANCE
IRL
IRELAND
JPN
JAPAN
MAR
MOROCCO
NZL
NEW ZEALAND
trip table
trip_id
country_code
country_code2
country_code3
1
FRA
IRL
JPN
2
MAR
NZL
My goal is to have country names displayed on the trip table instead of country codes.
I succeed to have only 1 country code replaced, thanks to the left join clause. I would like to have up to 3 country names displayed per row.
SELECT trip_id, country_name
FROM trip
LEFT JOIN country ON country_code = country_name
The actual output of the trip table:
trip_id
country_name
1
FRANCE
2
MOROCCO
Is there a way to replace each country code with its corresponding country name?
The EXPECTED output of the query from the trip table:
trip_id
country_name
country_name2
country_name3
1
FRANCE
IRELAND
JAPAN
2
MOROCCO
NEW ZEALAND
Thank you!
You could add two more joins
SELECT trip_id, c1.country_name, c2.country_name, c3.country_name
FROM trip t
left join
country c1
on t.country_code = c1.country_code
left join
country c2
on t.country_code2 = c2.country_code
left join
country c3
on t.country_code3 = c3.country_code
The cleanest way of accomplishing this query is using subqueries:
SELECT t.trip_id,
(SELECT country_name FROM country WHERE country_code = t.country_code) "c1",
(SELECT country_name FROM country WHERE country_code = t.countty_code2) "c2",
(SELECT country_name FROM country WHERE country_code = t.country_code3) "c3"
FROM trip t

How group by count from multiple tables

I have 3 different tables, country, city, and customer. Tables are shown below:
country table:
id country_name
1 UK
2 US
3 Brazil
:
n Canada
city table
id city_name postal_code country_id
1 London 30090 1
2 Dallas 20909 2
3 Rio 29090 3
4 Atlanta 30318 2
:
n Vancouver 32230 n
customer table
id customer_name city_id
1 John 1
2 Pete 3
3 Dave 2
4 May 2
5 Chuck 4
6 Sam 3
7 Henry 3
***country.id is references city.country_id, and city.id is references customer.city_id
I want to write a query that can extract the country name, city name and the count of the customer of the associate city. But with one condition, the query will return all cities with more customers than the average number of customers of all cities
It will look something like below, this is the correct output
UK London 2
Brazil Rio 3
but I kept getting this output, which isn't correct
UK London 2
US Dallas 2
US Atlanta 1
Brazil Rio 3
I fixed my SQL query but it doesn't give me the result that I want
SELECT country.country_name, city.city_name, COUNT(customer.city_id) from country
JOIN city on country.id = city.country_id
JOIN customer on city.id = customer.city_id
Group by city_name,country.country_name;
I am wondering how can I do this and fix my code?
add country.country_name in group by
SELECT country.country_name, city.city_name, COUNT(customer.city_id) from country
JOIN city on country.id = city.country_id
JOIN customer on city.id = customer.city_id
Group by city_name,country.country_name
You are missing country.country_name in the query it will give you error. As a general rule all columns on which aggregate function is not applied should be part of group by clause.
So either you write your query without country_name in the select or add country_name in the group by clause.

Query to output customers and employees and their cities where each city must contain at least one customer and one employee

I must take sellers and customers and output them in one column, showing their cities and types.
My problem is: I don't need to output customers whose cities aren't in sellers' table and vice versa.
SELECT
ContactName, City, Type
FROM
(SELECT
'Seller' AS Type,
ContactName, City
FROM
[dbo].[Suppliers] t
GROUP BY
City, ContactName
UNION
SELECT
'Customer',
ContactName, City
FROM
[dbo].[Customers] t
GROUP BY
City, ContactName) t
GROUP BY
ContactName, City, Type
Result :
| Ivan Ivanov | Seller | Moscow |
| Piotr Petrov | Seller | Moscow |
| Ivan Romanov | Customer | Moscow |
| Johnny Bravo | Customer | London |
(let's assume there are no sellers in London therefore this column shouldn't exist)
Expected result: only columns with information where a city has at least one seller and one customer grouped by contact name and city
This seems like union all and exists:
SELECT DISTINCT c.ContactName, 'Customer' as type, c.City
FROM Customers c
WHERE EXISTS (SELECT 1 FROM Sellers s WHERE s.city = c.city)
UNION ALL
SELECT DISTINCT s.ContactName, 'Seller' as type, s.City
FROM Sellers s
WHERE EXISTS (SELECT 1 FROM Customers c WHERE c.city = s.city);
I'm not sure the SELECT DISTINCT is really needed -- I don't see why the underlying tables would have duplicates (although ContactName is not really a good column for unique identification). However, your original query has GROUP BY, suggesting that you want to eliminate duplicates.

how to SQL query with conditioned distinct

Simple Database:
street | age
1st st | 2
2nd st | 3
3rd st | 4
3rd st | 2
I'd like to build a query that'll return the DISTINCT street names, but only for those households where no one is over 3.
so that result would be:
street | age
1st st | 2
2nd st | 3
How do I do that? I know of DISTINCT, but now how to conditionalize it for all the records that match the DISTINCT
Suppose the name of the table is 'tab'. You can then try:
select distinct street from tab where street not in (select street from tab where age>3);
I have created a sql fiddle where you can view the result:
http://sqlfiddle.com/#!9/2c513d/2
Distinct street names for households where no one is over 3:
SELECT street
FROM table
GROUP BY street
HAVING COUNT(1) <= 3
SELECT DISTINCT street
FROM table
WHERE NOT(age>3)
USE GROUP BY
Select Street
from yourtable
group by street
Having sum(age)<=3
Another way this could be achived with a use of NOT EXISTS
SELECT *
FROM yourtable a
WHERE NOT EXISTS
(SELECT street
FROM yourtable b
WHERE age > 3
AND a.street = b.street)

Select Statement to return TaskID if column Exists in another table

I have two tables.
First table is called task the second table is named countries.
My task table
ID TaskID Country CustomerID
------------------------------------------
1 213 china 22
2 213 USA 24
3 213 china 26
4 214 Canada 28
Countries table
ID Country
---------------
1 USA
2 Japan
3 England
I need a select statement that returns all task ID's that doesnt have its country i the countries table.
In this case I would need to return TASKID: 214 because canada is not in the countries table. I would not get TASKID: 213 because USA is in the countries table.
Try this:
SELECT
TaskID
FROM
Task T LEFT OUTER JOIN Countries C ON (T.COUNTRY = C.COUNTRY)
GROUP BY
TaskID
HAVING
COUNT(C.ID) = 0
Try this,
select taskid
from task where taskid not in
(select taskid from task where country in
(select country from countries))
Try like this,
SELECT t.taskid
FROM task t
WHERE EXISTS (
SELECT 1
FROM countries
WHERE country = t.country
)