Ashu96 Posted February 9, 2022 Share Posted February 9, 2022 Hi everyone, I am currently learning SQL and was going through self join tutorial. The command for the same was as follows: The output is attached below. However there will always be duplicates in this output. How shall I remove these duplicate rows after joining them ? Link to comment Share on other sites More sharing options...
niche Posted February 11, 2022 Share Posted February 11, 2022 https://www.sqlshack.com/different-ways-to-sql-delete-duplicate-rows-from-a-sql-table/ Link to comment Share on other sites More sharing options...
Ingolme Posted February 12, 2022 Share Posted February 12, 2022 There's no efficient way to solve it. Here's one possible solution, but this query is really inefficient because it's doing string operations on every single row. SELECT A.CustomerName AS CustomerName1, B.CustomerName as CustomerName2, A.City, CASE WHEN A.CustomerName < B.CustomerName THEN CONCAT(A.CustomerName,B.CustomerName) ELSE CONCAT(B.CustomerName, A.CustomerName) END AS DuplicateTest FROM Customers A, Customers B WHERE A.CustomerID <> B.CustomerID AND A.City = B.City GROUP BY DuplicateTest ORDER BY A.City 6 hours ago, niche said: https://www.sqlshack.com/different-ways-to-sql-delete-duplicate-rows-from-a-sql-table/ None of the solutions in that article apply here, because the rows shown in the first post aren't actually duplicates by SQL standards. The topic creator is saying that a row [ X Y ] is a duplicate of [ Y X ], but the values in each of the two columns are different from one row to the next. 1 Link to comment Share on other sites More sharing options...
Ashu96 Posted February 12, 2022 Author Share Posted February 12, 2022 Thanks @Ingolme for the answer. Yes it's not efficient but gives the desired answer. Appreciate your explanation of the answer as well as of @niche's reply. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now