I have database with columns
ID FirstName LastName. I would like get the id of various people. Hence I am writing queries like the below.
Select ID from Database where FirstName='X' and LastName='x1'
Select ID from Database where FirstName='Y' and LastName='y1'
Select ID from Database where FirstName='Z' and LastName='z1'
I there any way to optimize this query.
You can simply put multiple condition in single where clause so just use them for simplicity.
Select ID from Database
where (FirstName='X' and LastName='x1') OR
(FirstName='Y' and LastName='y1') OR
(FirstName='Z' and LastName='z1')
with NULL Entries- You can add one more condition
OR (FirstName is NULL AND LastName is NULL)
If you want to get row even for those name which are not exists in your Database table (don't use this name for your table, BTW):
select a.FirstName, a.LastName, d.id
from (
select 'X', 'X1' union all
select 'Y', 'Y1' union all
select 'Z', 'Z1'
) as a(FirstName, LastName)
left outer join Database as d on d.FirstName = a.FirstName and d.LastName = a.LastName
Related
Emp(sid(pk) : integer, sname: varchar(255))
Dep(sid(fk) : integer, dep : varchar(255))
SQL:How I find the names of each employees who works in both ‘IT’ and ‘SE’?
To observe a query that Joins two tables together and get common values depend on a common column Ex: id, using INNTER JOIN will help you on that
The INNER JOIN keyword selects records that have matching values in both tables.
Solution
SELECT Emp.sid, Emp.sname FROM Emp
INNER JOIN
(SELECT sid FROM Dep WHERE dep='IT'
INTERSECT
SELECT sid FROM Dep WHERE dep='SE') as A
ON Emp.sid = A.sid
References
SQL INNER JOIN
The way I understand it, the data situation is as below. emp with sid and sname, and dep, with sid - the foreign key to emp and dep, this time not as a table, but a column containing the department's abbreviation. And the combination, in the dep table, of sid and dep, is unique.
If that is the constellation, then join the two tables using sid, filter by: dep in the set:('IT' , 'SE'); Then, put the two columns from emp into the the column list, and GROUP BY them, and finally, apply the grouping filter HAVING COUNT(*) = 2 to just get the group that has two entries when filtered by the two departments.
WITH
emp(sid, sname) AS (
SELECT 42,'Arthur'
UNION ALL SELECT 43,'Ford'
UNION ALL SELECT 44,'Zaphod'
)
,
dep(sid, dep) AS (
SELECT 42,'IT'
UNION ALL SELECT 42,'SE'
UNION ALL SELECT 42,'AC'
UNION ALL SELECT 43,'IT'
UNION ALL SELECT 43,'AC'
UNION ALL SELECT 44,'SE'
UNION ALL SELECT 44,'SA'
)
SELECT
emp.sid
, emp.sname
FROM emp JOIN dep USING(sid)
WHERE dep.dep IN ('IT','SE')
GROUP BY
emp.sid
, emp.sname
HAVING COUNT(*) = 2;
-- out sid|sname
-- out 42|Arthur
I'm trying to find the difference between three tables, a, b, and c. I'd like to join all three, showing nulls where each table does not have records.
Each table has two columns that I would like to join on, lets say first and last name.
So far, this is what I'm doing.
Select * from a
FULL OUTER JOIN b on
a.firstName = b.firstName AND a.lastname = b.lastname
FULL OUTER JOIN c on
(c.firstName = a.firstName and c.lastName = a.LastNAME) OR
(c.firstName = b.firstName AND c.lastname = b.LastName)
I'm not confident that the logic in my output is correct. Is there a better way of doing this? In english, the first join joins A and B, including records in each table that don't exist in the other table. The second join joins A+B to C, joining C to either A or B if it matches.
You may want:
select firstname, lastname, sum(in_a), sum(in_b), sum(in_c)
from ((select firstname, lastname, 1 as in_a, 0 as in_b, 0 as in_c
from a
) union all
(select firstname, lastname, 0 as in_a, 1 as in_b, 0 as in_c
from b
) union all
(select firstname, lastname, 0 as in_a, 0 as in_b, 1 as in_c
from c
)
) abc
group by firstname, lastname;
This should each firstname/lastname pair with information about how many times the pair is in each table.
I prefer the union all/group by approach for this over left join for several reasons:
Without the using clause, the on clause gets a bit tricky as you add more tables.
Minimal use of coalesce().
Better handles duplicates within each table.
Handles NULL values for the keys.
Here are the two table structures
I have two tables where I am trying to fetch some duplicate records based on some condition like where a.fname=b.fname and a.phone_no<>b.phone_no
But also I need to include other column 'address' which is in table 2 and introduce the same condition for duplicate checking for address as well.
SELECT
"Fname"||' '||"Lname" AS "Customer_Name",
COUNT(*) AS "Countof"
FROM "S_CONTACT" A
WHERE EXISTS (
SELECT 1
FROM "S_CONTACT" B
WHERE A."PHONE" != B."PHONE"
AND A."Fname" = B."Fname"
AND A."EMAIL"=B."EMAIL"
AND A."Lname"=B."Lname"
AND "DOB" IS NULL
)
GROUP BY "Fname","Lname","EMAIL"
HAVING count(*) >1;
The above sql gives me a list of customers with duplicate names and email.
But I do not know how to introduce the column address in this sql which is from different table t2
If you are trying to find duplicates in one table:
select c.fname, c.lname, c.email
from contacts c
group by c.fname, c.lname, c.email
having min(c.phone) <> max(c.phone);
If you want to count null as a different value, then use:
having min(c.phone) <> max(c.phone) or count(c.phone) <> count(*)
You can do the same thing on the second table:
select c.fname, c.lname, c.email
from second_table c
group by c.fname, c.lname, c.email
having min(c.address) <> max(c.address) or count(c.address) <> count(*)
If you need the results in the same result set, then use union or union all or some similar mechanism.
I have a table UserParent:
Id, FirstName, LastName
I have a table UserChild:
Id, ParentUserId (FK), ChildAttributeX
I have the following sample SQL:
SELECT Id, 0 ChildUserId, FirstName, LastName, NULL FROM UserParent
UNION
SELECT ParentUserId, Id, FirstName, LastName, ChildAttributeX FROM UserChild
Some Users may exist in both tables. All Users are stored with basic info in UserParent although some Users who have ChildAttributeX will have a FK ref to the UserParent in UserChild along with the ChildAttributeX in UserChild.
How can I resolve this as part of a UNION or some other SQL technique so all Users are included in the result set, without duplicate users?
I think this is what you are looking for. If all records must exist in parent table, this will return all records from parent, and any record that exist in child table, but only unique records (DISTINCT does that).
SELECT DISTINCT UP.ID, UP.FirstName, UP.LastName
FROM UserParent UP
LEFT OUTER JOIN UserChild UC ON UP.ID = UC.ParentUserID
If you are looking for all the records present in both table, you can try below query:
SELECT
coalesce(UP.Id,UC.ParentUserId),
0 ChildUserId,
(UP.FirstName,UC.FirstName),
(UP.LastName,UC.LastName),
NULL FROM
UserParent UP
FULL OUTER JOIN
UserChild UC
ON UC.ParentUserId = UP.ID
I have a problem that I can't seem to solve. What I am trying to do is find unique columns based in table A that doesn't exist in other table B and store them in table B. Something on the lines of
SELECT DISTINCT(A.FNAME), A.LNAME from ADDRESS A WHERE NOT EXIST
(SELECT DISTINCT(B.FNAME),B.LNAME
FROM ADDRESSLIVE B)
but that doesn't seem to work, my ideal logic is to use the FNAME column and LNAME column together as a unique id, since those columns separately can be duplicates. Can someone inform me of what I am doing wrong, or what I am trying to do if its even possible?
SELECT DISTINCT A.FName, A.LName FROM Address A
WHERE NOT EXISTS
(SELECT * FROM AddressLive B WHERE B.FName = A.FName AND B.LName = A.LName)