SQL server where clause (compare more values in more values) - sql

SELECT * FROM Customers
WHERE ('Paris','London', 'Dublin', 'Venice') in (SELECT city FROM Europe)
SELECT city FROM Europe return more than one value,
how can I do this?
i dont want to repeat the condition like:
WHERE ('Paris') in (SELECT city FROM Customers)
or ('London') in (SELECT city FROM Customers)
or ('Dublin') in (SELECT city FROM Customers) or ....

Your second select is redundant, and you can just "flip" in condition.
So equivalent of your query is:
SELECT * FROM Customers
WHERE city in ('Paris','London', 'Dublin', 'Venice')
Update.
Based on logic you've disclosed in comments, you query should be something like:
select * from Customers
where exists(select top 1 * from Europe where city in ('Paris','London', 'Dublin', 'Venice'))
Exactly as was stated: if exists at least one of city in ('Paris','London', 'Dublin', 'Venice') in Europe then everything from Customers will be selected, else nothing will be selected.

You can just use in and the column name. IN is equivalent to or, which is city = 'Paris' or city = 'London'.. in your case.
SELECT * FROM Customers
WHERE city in ('Paris','London', 'Dublin', 'Venice')
Edit: Using exists
SELECT c.* FROM Customers c
WHERE exists (select 1 from Europe where city = c.city
and city in ('Paris','London', 'Dublin', 'Venice')

Related

SQL query to list cities in which employee did not work

SQL query to list cities in which employee did not work from below
"employee" table:
name
city
srini
seattle
ross
atlanta
rich
redmond
Example: if I give "srini", query should return "Atlanta" and "Redmond"
I tried below 2 queries with no luck, it returns empty results:
SELECT t1.city
FROM employee t1
JOIN employee t2 ON t1.name=t2.name
WHERE t1.city != t2.city
WHERE name='srini'
SELECT city
FROM (SELECT city FROM employee WHERE name='srini') as e1
WHERE city <> e1.city
This should work:
select distinct city
from employee
where city not in
(select city
from employee
where name = 'srini')
Basically it's selecting all city names that don't exist in a row where name is 'srini'
SQL is such fun. I'd go with a GROUP BY query, where I use the HAVING clause to only return cities where no srini lives.
select city
from employee
group by city
having sum(case when name = 'srini' then 1 else 0 end) = 0
Core ISO/ANSI SQL, i.e. every dbms is expected to support it.
Or use EXCEPT:
select city from employee
EXCEPT
select city from employee where name = 'srini'
Core ISO/ANSI SQL, i.e. every dbms is expected to support it.

Count city and State from 2 different tables

I need to count the number of cities occurrences on 2 different tables. One Table is the Supplier table which has supplier_id, City and State. The second table is consumer with consumer id , City and State. My query needs to return the State, City number of supplier cities and number of consumer cities where the names match.
I have tried a couple different things like intersect and union all but cant get it My latest is below but it is Not sure what I am doing wrong
SELECT S.State
,C.City
,count(S.City) as Number_Of_Suppliers
,count(C.City) as Number_Of_Consumers
from dbo.Tb_Supplier S
left outer Join dbo.Tb_Consumer C
On S.STATE = C.STATE
AND S.City = C.City
group by S.state
,C.City
Use union all and group by:
select state, city, sum(supplier)
sum(supplier) as Number_Of_Suppliers,
sum(consumer) as Number_Of_Consumers
from ((select state, city, 1 as supplier, 0 as consumer
from dbo.Tb_Supplier s
) union all
(select state, city, 0 as supplier, 1 as consumer
from dbo.Tb_Consumer c
)
) sc
group by state, city

I am not sure about this query

query to extract the data from the customer table if and only if one or more of the customers in the customer table are located in London
and my query is
select * from Customer
where 'London' = ANY (select city from Customer)
I know it's not the correct way but I wanted to know if this also works or not.
TIA
Your query works, so that is one way to express it (and rather clever). Here is an example.
More commonly, I think of this as anexists query:
select c.*
from Customer c
where exists (select 1 from Customer c2 where c2.city = 'London');
This will return all customers from any city, even those not in London.
It is quite possible that you just want to return customers in London, in which case a simple where city = 'London' suffices.
select * from Customer where city ='London' and 1 > (select count(*) from Customer where city='London');

How can I write SQL Query?

I need a request that displays the name and surname of the clients with the smallest credit limit - among married women who do not live in Japan, Brazil or Italy.
Diagram:
This will Give all the people not in 'Japan', 'Brazil' or 'Italy') ,
Select C.Cust_first_name,C.Cust_Last_name from Customers C
Inner Join Countries C1
on C.Country_Id=C1.Country_Id
Where C1.Country_Name Not in('Japan', 'Brazil' or 'Italy')
and
C.Cust_Credit_Limit=(Select Min(Cust_Credit_Limit) From from Customers C)
If we Convert The Question to Code that will be the above code,
The Script would return the name and last name of person not in ('Japan', 'Brazil' or 'Italy') And has the lowest salary In the entire customer Base.
select top 1 * from Customers C
Inner Join
(
select MIn(cust_credit_limit) from Customers C1
Inner Join Countries CT on C1.Country_id = C1.Country_id
where CT.Country_Name Not in ('Japan', 'Brazil','Italy')
)
C2 on C2.cust_credit_limit = C.cust_credit_limit

How to write query in the following case?

I have the following table. How do I query
Table Team is as follows:
ID,Name,City,League
1,Name1,City1, A
2,Name2,City1, B
The trick is to get a COUNT(DISTINCT League) per city, and compare that number to the total number of leagues COUNT(DISTINCT League) across the whole table.
SELECT
City,
COUNT(DISTINCT League) AS numleagues
FROM yourtable
GROUP BY City
/* Only cities which have the same number of distinct leagues as the total number of distinct leagues */
HAVING COUNT(DISTINCT League) = (SELECT COUNT(DISTINCT League) FROM yourtable)
Here it is in action in SQLFiddle
All cities for which there does not exist a League which is not in the list of Leagues associated with the city:
SELECT DISTINCT City FROM Teams T1 WHERE NOT EXISTS
(SELECT * FROM Teams T2 WHERE League NOT IN
(SELECT League FROM Teams T3 WHERE T3.City = T1.City))
Almost exactly as you worded it in English, but with a twist... You want all cities that have a Name in all leagues, or to rephrase, you want all Cities where there does not exist a League that does not have a name in it from that city.;..
Select Distinct City From Table t
Where Not Exists
(Select Distinct League From Table L
Where Not Exists
(Select * From Table
Where City = t.City
And League = L.League
And Name Not in
(Select distinct Name from table
Where City = t.City) ))