Search
 
SCRIPT & CODE EXAMPLE
 

SQL

how to query without duplicate rows in sql

SELECT DISTINCT col1,col2... FROM table_name where Condition;
Comment

Query to remove duplicate rows from a table

DELETE FROM Customers WHERE ROWID(SELECT MAX (rowid) FROM Customers C WHERE CustomerNumber = C.CustomerNumber);
Comment

sql delete duplicate rows

WITH cte AS (
    SELECT 
        contact_id, 
        first_name, 
        last_name, 
        email, 
        ROW_NUMBER() OVER (
            PARTITION BY 
                first_name, 
                last_name, 
                email
            ORDER BY 
                first_name, 
                last_name, 
                email
        ) row_num
     FROM 
        sales.contacts
)
DELETE FROM cte
WHERE row_num > 1;
Comment

sql query duplicate rows

/* This SQL Query will show each of those duplicate
rows individually instead of just grouping it */
SELECT username,email 
FROM `users` 
WHERE `username` 
IN (SELECT username FROM `users` GROUP BY username HAVING COUNT(username) > 1)
Comment

sql delete duplicate rows but keep one

# Step 1: Copy distinct values to temporary table
CREATE TEMPORARY TABLE tmp_user (
    SELECT id, name 
    FROM user
    GROUP BY name
);

# Step 2: Remove all rows from original table
DELETE FROM user;

# Step 3: Remove all rows from original table
INSERT INTO user (SELECT * FROM tmp_user);

# Step 4: Remove temporary table
DROP TABLE tmp_user;
Comment

sql duplicate rows

/*SELECT hotel_id, reservation_id, COUNT(hotel_id)FROM staywhere  reservation_id is not nullGROUP BY hotel_id, reservation_idHAVING COUNT(hotel_id) > 1*/select distinct s.hotel_id , t.* from stay sright join (    select hotel_id, reservation_id, count(*) as qty    from staywhere reservation_id !=1      group by hotel_id, reservation_id    having count(*) > 1) t on s.hotel_id = t.hotel_id and s.reservation_id = t.reservation_id
Comment

sql query to delete duplicate records

--ID should be primary key

--get duplicate records using RANK
SELECT E.ID, 
    E.firstname, 
    E.lastname, 
    E.country, 
    T.rank
FROM [SampleDB].[dbo].[Employee] E
  INNER JOIN
(
 SELECT *, 
        RANK() OVER(PARTITION BY firstname, 
                                 lastname, 
                                 country
        ORDER BY id) rank
 FROM [SampleDB].[dbo].[Employee]
) T ON E.ID = t.ID;

--delete duplications
DELETE E
    FROM [SampleDB].[dbo].[Employee] E
         INNER JOIN
    (
        SELECT *, 
               RANK() OVER(PARTITION BY firstname, 
                                        lastname, 
                                        country
               ORDER BY id) rank
        FROM [SampleDB].[dbo].[Employee]
    ) T ON E.ID = t.ID
    WHERE rank > 1;
Comment

PREVIOUS NEXT
Code Example
Sql :: sql server change schema of a table 
Sql :: postgres : ERROR: division by zero 
Sql :: mysql change timestamp on update 
Sql :: difference between super key and candidate key 
Sql :: update table from another table 
Sql :: how to identify locked tables in sql server 
Sql :: mysql delete duplicate rows but keep one 
Sql :: mysql get latest duplicate rows 
Sql :: mysql update from select on same table 
Sql :: sqlalchemy return id after insert 
Sql :: sql convert float to string 
Sql :: how to drop a unique constraint in sql 
Sql :: get data every 30 days in sql 
Sql :: How to add a Try/Catch to SQL Stored Procedure 
Sql :: primary key multiple 
Sql :: python sqlite3 update 
Sql :: how to know the character set of an oracle databes 
Sql :: install postgresql 10 centos 7 
Sql :: sqlite unique 
Sql :: join multiple tables sql 
Sql :: oracle shrink table 
Sql :: mysql remove auto increment 
Sql :: rename column in table sql 
Sql :: mssql datetime to date 
Sql :: power bi dax is in the last 3 months 
Sql :: Write an SQL query to print details of the Workers whose FIRST_NAME ends with ‘a’. 
Sql :: Save PL/pgSQL output from PostgreSQL to a CSV file 
Sql :: how to select random rows from a table 
Sql :: sql max of two values 
Sql :: how to write lowercase in sql 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =