Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Distinct in Join Query

in the below Query Benz table car_no is repeated. when I join and compare with other table car_no how do use distinct?

table 1: Cars
Table 2: benz

  join Benz bz on bz.car_no = cr.car_no 

in the above car_no values in the Benz table are repeated. in Cars table car_no values unique. then how to join with distinct in query.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

something i tried.

  join Benz bz on Distinct(bz.car_no) = cr.car_no 

getting error.

>Solution :

Please try this

SELECT cr.*, bz.*
FROM Cars cr
JOIN (
  SELECT DISTINCT car_no, other_columns
  FROM Benz
) bz
ON bz.car_no = cr.car_no;

In this example, the subquery ‘SELECT DISTINCT car_no, other_columns FROM Benz’ is used to select only the distinct ‘car_no’ values from the ‘Benz’ table. The subquery is then joined with the ‘Cars’ table using the ‘ON’ clause. The resulting query will only return rows where the ‘car_no’ values are unique.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading