Tutorial :SQL query to get all the combinations of two columns


Two days ago I was asked this question in an interview for Data Analyst position. Could some one please let me know the correct answer for this?

Say there is a single table with three columns.

  • 1st column with GeneId(primary key)
  • 2nd column Flag1,
  • 3rd column Flag2.

Flag1 and Flag2 columns can have values of 0 or 1. How do I write a single SQL query in which I get the count of GeneIds for different combinations of Flag1,Flag2 possible, i.e. Flag1 0, Flag2 1, Flag1 1, Flag2 0, ....& other combinations.

Thanks for your time,

Regards Sashi


SELECT Flag1, Flag2, COUNT(GeneId) as NumGenes  FROM genetable  GROUP BY Flag1, Flag2  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »