SQL - Finding rows with similar values for a column - mysql

ID NAME Email
1 John john#example.com
2 Mathew mathew#example.com
3 John jon#example.com
4 Johnson johns#example.com
5 Peter pete#example.com
How can I create a query that will return
1 John john#example.com
3 John jon#example.com
4 Johnson johns#example.com
Ansers like that for Find rows that have the same value on a column in MySQL returns rows with same value. Here I need similar values

Get all the records with similar email first and then only print those records that match the inner condition.
select * from table_name where email in (
select email from table_name
group by email
having count(*)>1
)
Tested and working fine.

Related

List of duplicate values SQL

Duplicate values can be found using this
SELECT email, COUNT(email)
FROM users
GROUP BY email
HAVING COUNT(email) > 1
for the data
ID NAME EMAIL
1 John asd#example.com
2 Sam asd#example.com
3 Tom asd#example.com
4 Bob bob#example.com
5 mob bob#example.com
But the table I need is
NAME EMAIL
John, Sam, Tom asd#example.com
Bob, mob bob#example.com
You need the GROUP_CONCAT() function, which IIRC works in both MySql and Sqlite, but handles NULL values different between the two (I may be wrong on that last part).
SELECT group_concat(name) as Name, email
FROM users
GROUP BY email
HAVING COUNT(email) > 1

Get unique data across certain column in multiple tables - sql

I know this is probably so odd to ask. But lets say I have 3 tables:
Table 1
ID
Name
1
Adam
2
David
3
Conor
Table 2
ID
Name
1
Adam
2
Derek
3
Niall
Table 3
ID
Name
1
Adam
2
David
3
John
Is there any way I can write a query to get the unique names across all 3 tables. So it would return "Adam, David, Conor, Derek, Niall, John"
Order doesn't matter
If it helps, all name values are related to a names table
yes , one way is to union them
select name from table1
union
select name from table2
union
select name from table3
union automatically removes duplicate cases

SQL: How to find entry that appears more than twice with specific value

I want to find table entry that appears more than once with certain value
Name | ID
==================
Peter 1
James 2
Peter 2
James 2
I want to select entries where ID is both 1 and 2, in this example only Peter appears twice with value 1 and 2 while James appear twice but ID of James is not 1 and 2
Is there operator for this kind of query?
You can do:
select name
from t
where id in (1, 2)
group by name
having count(distinct id) = 2;

MySQL get first non null value after group by

I have a large table with data that is not unique but needs to be. This table is a result of multiple union selects so is not an actual table. I cannot make it an actual table for other reasons.
All of the UNION'd tables have an email column which will eventually be unique. The resulting records look like this:
1 ozzy#test.com Ozzy
2 test#test.com Tony
3 test#yahoo.com Steve
4 tiny#test.com
13 tony#gmail.com Tony
14 test#test.com Ozzy
15 test#yahoo.com Dave
16 tiny#test.com Tim
As you can see, some emails appear more then once with different names or non-existent names. When I add a GROUP BY email clause at the end, the results look like this:
1 ozzy#test.com Ozzy
2 test#test.com Tony
3 test#yahoo.com Steve
4 tiny#test.com
13 tony#gmail.com Tony
As you can see, email 4 does not have a name because it chose the first entry with NULL for a name. Then I tried to use GROUP_CONCAT which made the results look like this:
1 ozzy#test.com Ozzy
14 test#test.com Ozzy,Tony
15 test#yahoo.com Dave,Steve
16 tiny#test.com Tim
13 tony#gmail.com Tony
As you can see, now everyone has a name but some rows have more then one name concatenated. What I want to do is GROUP BY email and choose the first NOT NULL entry of each column for each row to theoretically look like so:
1 ozzy#test.com Ozzy
2 test#test.com Tony
3 test#yahoo.com Steve
4 tiny#test.com Tim
13 tony#gmail.com Tony
I have tried using COALESCE but it doesn't work as intended. My current query looks like so:
SELECT
id,
email,
`name`
FROM
(
SELECT
email,
`name`
FROM
multiple_tables_and_unions
) AS emails
GROUP BY email
I have removed the code from the temporary table as it contains many tables but all select the email and name column. Essentially I need a function like GROUP_COALESCE but unfortunately it does not exist. What are my options?
Try using MAX, like this:
SELECT
email,
MAX(`name`)
FROM
(
SELECT
email,
`name`
FROM
multiple_tables_and_unions
) AS emails
GROUP BY email
To select the first non null value, GROUP_CONCAT can be handy here. Below is the example:
SELECT
email,
SUBSTRING_INDEX(GROUP_CONCAT(`name`), ',',1)
FROM
(
SELECT
email,
`name`
FROM
multiple_tables_and_unions
) AS emails
GROUP BY email;

Listing all rows that have a duplicate entry in a particular column in MySQL table

Despite a good deal of searching I haven't come up with the correct method of listing all columns for rows that have the same content in a particular column (let's say the column 'Name').
So if my table, called USERS, had the following content:
ID User Name
1 Nick Nick
2 NickP Nick
3 NickC Nick
4 John John
5 Brian Brian
The SELECT statement should return:
ID User Name
1 Nick Nick
2 NickP Nick
3 NickC Nick
As these are all the rows that contain the same content in column 'Name'. How would I write this?
How about this?
SELECT * FROM Users WHERE Name IN(SELECT Name FROM Users GROUP BY Name HAVING COUNT(1) > 1)