how to compare multiple rows in where clause

pk786
Given table:

create table borrower (
customer_number char ( 1 0 ) ,
loan_number char ( 1 0 ));

question: Find the customer number (i.e., ID) of every customer that shares all of the loans that customer “1234” has. If the customer whose customer number is “1234” has loans “L1” and “L2”, you need to find all customers (including customer “1234”) that share both “L1” and “L2”.

I have created 4 entries in the table for the columns (customer_number, loan_number) are (1234, L1) (1234, L2) (1, LI)(1, L2)(2, L1). I am using query

select distinct customer_number 
from borrower
where loan_number IN(
    select loan_number 
    from borrower 
    where customer_number='1234'

but it is retrieving output such as

1234, 1, 2 

I want only 1 and 1234(Customer_number) in my output because it is the only one having both loan number L1 and L2. Cutsomer_number "2" is not associated with both the loan_number, so it should come in the out put.

I have also tried "all" in place of "IN".

select distinct customer_number 
    from borrower
    where loan_number= all(
        select loan_number 
        from borrower 
        where customer_number='1234'

output: nothing

PeteH

You could use a temporary table and a join.

/* create a table of the loans we are looking for */
create table Temp select distinct loan_number from Borrower 
where customer_number = '1234';

/* join against the temp table and select borrowers with 3 matches */
select customer_number as 'Match' 
from (select customer_number, COUNT(*) as C 
         from Borrower 
         inner join Temp on Borrower.loan_number = Temp.loan_number
         group by customer_number) X
where C = 3

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to compare multiple dates in where clause in sql

From Dev

How to delete multiple rows from multiple tables using Where clause?

From Dev

How to compare Timestamp in where clause

From Dev

How to compare with Case in Where clause

From Dev

How to update multiple rows using single where clause

From Dev

How to compare multiple rows?

From Dev

Update multiple rows with where clause in codeigniter

From Dev

Multiple condition WHERE clause returning unnecessary rows

From Dev

Single column for multiple rows within WHERE clause

From Dev

How to compare int with string in linq where clause

From Dev

If a WHERE clause applies to multiple rows, are all rows tested?

From Dev

How do I fetch match a WHERE clause across multiple rows of the same table?

From Dev

Android SQLiteDatabase update multiple rows using where clause with IN or OR

From Dev

Deleting multiple rows based on where clause using FluentMigrator

From Dev

MySQL returning false when multiple rows meet WHERE clause

From Dev

How do I reference 2 aliases and compare them in WHERE clause?

From Dev

How do I reference 2 aliases and compare them in WHERE clause?

From Dev

How to compare the values for multiple rows in SQL

From Java

Postgres where clause compare timestamp

From Dev

Where clause in Linq and compare strings

From Dev

How to compare multiple checkboxlist values in LINQ like IN clause SQL

From Dev

Multiple where in clause in Mysql

From Dev

Where clause with multiple conditions

From Dev

Multiple Where clause with decryption

From Dev

Multiple queries in WHERE clause

From Dev

Multiple AND in where clause

From Dev

Where clause with multiple OR

From Dev

how can i use multiple where clause In codeigniter?

From Java

How to Create Multiple Where Clause Query Using Laravel Eloquent?

Related Related

  1. 1

    how to compare multiple dates in where clause in sql

  2. 2

    How to delete multiple rows from multiple tables using Where clause?

  3. 3

    How to compare Timestamp in where clause

  4. 4

    How to compare with Case in Where clause

  5. 5

    How to update multiple rows using single where clause

  6. 6

    How to compare multiple rows?

  7. 7

    Update multiple rows with where clause in codeigniter

  8. 8

    Multiple condition WHERE clause returning unnecessary rows

  9. 9

    Single column for multiple rows within WHERE clause

  10. 10

    How to compare int with string in linq where clause

  11. 11

    If a WHERE clause applies to multiple rows, are all rows tested?

  12. 12

    How do I fetch match a WHERE clause across multiple rows of the same table?

  13. 13

    Android SQLiteDatabase update multiple rows using where clause with IN or OR

  14. 14

    Deleting multiple rows based on where clause using FluentMigrator

  15. 15

    MySQL returning false when multiple rows meet WHERE clause

  16. 16

    How do I reference 2 aliases and compare them in WHERE clause?

  17. 17

    How do I reference 2 aliases and compare them in WHERE clause?

  18. 18

    How to compare the values for multiple rows in SQL

  19. 19

    Postgres where clause compare timestamp

  20. 20

    Where clause in Linq and compare strings

  21. 21

    How to compare multiple checkboxlist values in LINQ like IN clause SQL

  22. 22

    Multiple where in clause in Mysql

  23. 23

    Where clause with multiple conditions

  24. 24

    Multiple Where clause with decryption

  25. 25

    Multiple queries in WHERE clause

  26. 26

    Multiple AND in where clause

  27. 27

    Where clause with multiple OR

  28. 28

    how can i use multiple where clause In codeigniter?

  29. 29

    How to Create Multiple Where Clause Query Using Laravel Eloquent?

HotTag

Archive