How do I select only people who share last names? SQL

nymkalahi

I'm still pretty new to working with databases, and this problem has me stumped.

I've got a People table with first and last name columns. I'm trying to create a query that will only select those who share last names with others in the table.

For example if I have these two columns:

First_Name      Last_Name
John            Doe
Jane            Doe
Mary            Shmo
Kate            Shmo
Matt            Diego
Joe             Smith

The result I want would be:

First_Name  Last_name
John        Doe
Jane        Doe
Mary        Shmo
Kate        Shmo

The code I have is:

select count(*), last_name, first_name 
from people
group by last_name
having count(*) > 1

This gets the shared last names, but only outputs one of each, instead of all the first names as well.

I'm sure there is a simple fix for this, but I can't figure it out.

Joe Enos

You're almost there. Now that you have the set of last names you care about, just wrap another query around that:

select * from people
where last_name in
(
    select last_name
    from people
    group by last_name
    having count(*) > 1
)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Select only elements who directly contain text

来自分类Dev

How do I count instances of values in columns by individual column using only SQL?

来自分类Dev

Retrieving people who +1 an URL

来自分类Dev

How to edit this mySQL query so as to find people who visited specific page more than x times?

来自分类Dev

How do I calculate an average date in SQL?

来自分类Dev

Azure and TFS, how do I share my project with a new developer joining the team

来自分类Dev

How do I only read the header for a URL in Ruby?

来自分类Dev

How do I create a read-only ng-repeat?

来自分类Dev

How do I get a list of all the GitHub projects I've contributed to in the last year?

来自分类Dev

How do I highlight (select) text in Text widget with a button click?

来自分类Dev

How do I select a subset of columns with diesel-rs?

来自分类Dev

How do I take the last so many entries (skip) with Eloquent Query Builder in Laravel

来自分类Dev

How do I execute a function after 5 sec from last keyup?

来自分类Dev

For each unique value of some groupid column, how do I get rows with last 3 dates?

来自分类Dev

How can I find who pushed a tag(s) to BitBucket

来自分类Dev

How do I create a Python CLR procedure in SQL Server?

来自分类Dev

How do I store an unsigned int in postgres sql?

来自分类Dev

How do I aggregate over ordered subsets of rows in SQL?

来自分类Dev

是否可以在SQL中将SELECT函数与LAST函数结合使用?

来自分类Dev

When using (substack's) Tape module for testing, how do I run only one test in a file?

来自分类Dev

How do I select list of generic objects in abstract service using CriteriaQuery

来自分类Dev

Laravel - How do I select all where column is equals to x or y or z (and on..)?

来自分类Dev

How Do I Structure Multiple Select Options Over an Array of Objects in Angular

来自分类Dev

how to call the web method only when '/' is the last character from autocomplete?

来自分类Dev

Nil param value is causing a crash. How do I only create a param if the value I'm setting it with exists?

来自分类Dev

Select last XML node

来自分类Dev

How do I check if a certain User exists under a specific Login in SQL?

来自分类Dev

如何从“ last”和“ who”命令读取ssh输出

来自分类Dev

How can I exclude last two child elements with CSS selector?

Related 相关文章

  1. 1

    Select only elements who directly contain text

  2. 2

    How do I count instances of values in columns by individual column using only SQL?

  3. 3

    Retrieving people who +1 an URL

  4. 4

    How to edit this mySQL query so as to find people who visited specific page more than x times?

  5. 5

    How do I calculate an average date in SQL?

  6. 6

    Azure and TFS, how do I share my project with a new developer joining the team

  7. 7

    How do I only read the header for a URL in Ruby?

  8. 8

    How do I create a read-only ng-repeat?

  9. 9

    How do I get a list of all the GitHub projects I've contributed to in the last year?

  10. 10

    How do I highlight (select) text in Text widget with a button click?

  11. 11

    How do I select a subset of columns with diesel-rs?

  12. 12

    How do I take the last so many entries (skip) with Eloquent Query Builder in Laravel

  13. 13

    How do I execute a function after 5 sec from last keyup?

  14. 14

    For each unique value of some groupid column, how do I get rows with last 3 dates?

  15. 15

    How can I find who pushed a tag(s) to BitBucket

  16. 16

    How do I create a Python CLR procedure in SQL Server?

  17. 17

    How do I store an unsigned int in postgres sql?

  18. 18

    How do I aggregate over ordered subsets of rows in SQL?

  19. 19

    是否可以在SQL中将SELECT函数与LAST函数结合使用?

  20. 20

    When using (substack's) Tape module for testing, how do I run only one test in a file?

  21. 21

    How do I select list of generic objects in abstract service using CriteriaQuery

  22. 22

    Laravel - How do I select all where column is equals to x or y or z (and on..)?

  23. 23

    How Do I Structure Multiple Select Options Over an Array of Objects in Angular

  24. 24

    how to call the web method only when '/' is the last character from autocomplete?

  25. 25

    Nil param value is causing a crash. How do I only create a param if the value I'm setting it with exists?

  26. 26

    Select last XML node

  27. 27

    How do I check if a certain User exists under a specific Login in SQL?

  28. 28

    如何从“ last”和“ who”命令读取ssh输出

  29. 29

    How can I exclude last two child elements with CSS selector?

热门标签

归档