how to find people with same family name? - sql

You have a table with 4 columns:
Primary key / name / surname / middle name
How to write Sql query to find people who has same family name?
1 / Ivan / Ivanov / Ivanovich
2 / Petr / Levinsky / Aleksandrovich
3 / Alex / Ivanov / albertovich
Should return Ivan and Alex
Thanks

In standard SQL you can simply join the table with itself:
select a.name, b.name
from t as a, t as b
where a.surname = b.surname and a.id < b.id
where t is your table and id is the primary key column.
This returns all distinct pairs of first names for every surname that has multiple entries.
You might want to add surname to the list of selected columns.

If you want to find exactly names then you should firstly find all surnames that appear more than once and the find all names:
select name
from t
where surname in (select surname from t group by surname having count(surname) > 1);

As for me easiest way is to group records by surname and then select those with count more than 1.

You want to GROUP BY the surname and then use a HAVING clause to find any groups that have > 1.
Untested:
SELECT
name
FROM
theTable
WHERE Surname IN (
SELECT
Surname
FROM
theTable
GROUP BY
Surname
HAVING
COUNT(Surname) > 1)

select surname,group_concat(firstname)
from people
group by surname
having count(firstname)> 1;

Related

How to select the record with two different postcode in ms access with sql query

I'm new in sql script. I'm currently trying to write a sql script to select the record that with two different postcode from a table in ms access with sql query. For some reason this is just not working for me.
Example:
I have the following table resident_postcode :
Postcode Name
1001 Alan
1002 James
1003 Alan
1004 Merry
1001 Merry
I write a sql script to select the name that have 2 different postcode and looking for the output like following:
Name
Alan
Merry
I have tried to run the following script but for some reason this is just not working in MS Acess:
SELECT a.Name
FROM resident_postcode a
WHERE 1 < (SELECT count(b.Postcode) FROM resident_postcode b WHERE b.Name= a.NameGROUP BY b.Name)
Does anyone know what is going wrong with my script?
Try with:
SELECT [Name]
FROM resident_postcode
GROUP BY [Name]
HAVING Count(*) > 1
If there is no repetition of same postcode for any name then you can have names with two post codes as below
SELECT NAME FROM RESIDENT_POSTCODE
GROUP BY NAME
HAVING COUNT(POSTCODE)=2
But if any postcode can be specified more than once (for example 1001 is specified more than once for Alan) then you need to use below query:
select name from (
SELECT distinct NAME, postcode FROM RESIDENT_POSTCODE)
group by name
having count(*)=2
if you want all names having two or more than 2 postcode then use below query:
select name from (
SELECT distinct NAME, postcode FROM RESIDENT_POSTCODE)
group by name
having count(*)>=2
If you want 2 or more different post codes, then you can use:
SELECT NAME
FROM RESIDENT_POSTCODE
GROUP BY NAME
HAVING MIN(POSTCODE) <> MAX(POSTCODE);
If you want exactly two, then you can use:
SELECT NAME
FROM (SELECT DISTINCT NAME, POSTCODE
FROM RESIDENT_POSTCODE
) AS NP
GROUP BY NAME
HAVING COUNT(*) = 2;

Count function to find people with ten references

need a bit of help regarding university work. (I am using SQL developer)
Basically, the question is 'Find the different first keywords associated with references by prolific authors who have more than ten references'
So far I have only been able to do:
select surname, count(surname)from librarian.readings
group by surname
having count(surname) > 10
which gives me
SURNAME COUNT(SURNAME)
---------- --------------
White 16
Marble 11
Peuquet 14
Robinson 12
Rhind 15
However, this doesn't give me the keywords associated with it
select distinct surname,key1
from librarian.readings
but this just gives me too much information.
How do I do this?
This is assuming that surname is a unique field;
SELECT
r.surname
,r.key1
FROM librarian.readings r
INNER JOIN (select
surname,
count(surname) surname_count
from librarian.readings
group by surname) sub
ON r.surname = sub.surname
WHERE sub.surname_count > 10
If Surname isn't unique then you need to do this by the primary key on librarian.readings.
If all you need is the distinct keywords associated with .... then:
select distinct key1
from librarian.readings
group by surname
having count(*) > 10

SQL - Removing Duplicate without 'hard' coding?

Heres my scenario.
I have a table with 3 rows I want to return within a stored procedure, rows are email, name and id. id must = 3 or 4 and email must only be per user as some have multiple entries.
I have a Select statement as follows
SELECT
DISTINCT email,
name,
id
from table
where
id = 3
or id = 4
Ok fairly simple but there are some users whose have entries that are both 3 and 4 so they appear twice, if they appear twice I want only those with ids of 4 remaining. I'll give another example below as its hard to explain.
Table -
Email Name Id
jimmy#domain.com jimmy 4
brian#domain.com brian 4
kevin#domain.com kevin 3
jimmy#domain.com jimmy 3
So in the above scenario I would want to ignore the jimmy with the id of 3, any way of doing this without hard coding?
Thanks
SELECT
email,
name,
max(id)
from table
where
id in( 3, 4 )
group by email, name
Is this what you want to achieve?
SELECT Email, Name, MAX(Id) FROM Table WHERE Id IN (3, 4) GROUP BY Email;
Sometimes using Having Count(*) > 1 may be useful to find duplicated records.
select * from table group by Email having count(*) > 1
or
select * from table group by Email having count(*) > 1 and id > 3.
The solution provided before with the select MAX(ID) from table sounds good for this case.
This maybe an alternative solution.
What RDMS are you using? This will return only one "Jimmy", using RANK():
SELECT A.email, A.name,A.id
FROM SO_Table A
INNER JOIN(
SELECT
email, name,id,RANK() OVER (Partition BY name ORDER BY ID DESC) AS COUNTER
FROM SO_Table B
) X ON X.ID = A.ID AND X.NAME = A.NAME
WHERE X.COUNTER = 1
Returns:
email name id
------------------------------
jimmy#domain.com jimmy 4
brian#domain.com brian 4
kevin#domain.com kevin 3

write a query to identify discrepancy

I have a table with Student ID's and Student Names. There has been issues with assigning unique Student Id's to students and Hence I want to find the duplicates
Here is the sample Table:
Student ID Student Name
1 Jack
1 John
1 Bill
2 Amanda
2 Molly
3 Ron
4 Matt
5 James
6 Kathy
6 Will
Here I want a third column "Duplicate_Count" to display count of duplicate records.
For e.g. "Duplicate_Count" would display "3" for Student ID = 1 and so on. How can I do this?
Thanks in advance
Select StudentId, Count(*) DupCount
From Table
Group By StudentId
Having Count(*) > 1
Order By Count(*) desc,
Select
aa.StudentId, aa.StudentName, bb.DupCount
from
Table as aa
join
(
Select StudentId, Count(*) as DupCount from Table group by StudentId
) as bb
on aa.StudentId = bb.StudentId
The virtual table gives the count for each StudentId, this is joined back to the original table to add the count to each student record.
If you want to add a column to the table to hold dupcount, this query can be used in an update statement to update that column in the table
This should work:
update mytable
set duplicate_count = (select count(*) from mytable t where t.id = mytable.id)
UPDATE:
As mentioned by #HansUp, adding a new column with the duplicate count probably doesn't make sense, but that really depends on what the OP originally thought of using it for. I'm leaving the answer in case it is of help for someone else.

Select only one distinct column

I guess that is an easy question to solve, but I'm researching for an hour for it's answer.
I have a table with three columns: "id","id_x" and "name". But it's populated with repeated values, like this:
id id_x name
1 100 Name_aaa
2 100 Name_aaa
3 100 Name_aaa
4 100 Name_aaa*
5 101 Name_bbb
6 101 Name_bbb*
Ok, ok, I didn't create this table, and I can't modify them... I just wanna know what query I can execute to return only the values "100 - Name_aaa" and "101 - name_bbb"... Note that has an "*" after some names. I'd like to group only by the "id_x".
Is there any way to do it without using subqueries or joins?
Thanks in advance!
Somewhat untested, but this should do it
select id_x, min(name) as name
from table
group by id_x
Try this,
select distinct id_x , name from tablename
select distinct
id_x,
case when name ~ E'\\*$' then substring(name, 1, length(name)-1)
else name end
from t;
Or:
SELECT DISTINCT ON (id_x) id_x, name FROM table ORDER BY id_x, name;