Alasdair Macintyre

Freelance Web Developer based in Manchester, UK

  • Home
  • About
  • Github
  • Facebook
  • Instagram

SQL Snippets

Posted by Alasdair Macintyre on December 20, 2011
Posted in: Uncategorized.

Just some random snippets of SQL I always have to search to find (or spend an inordinate amount of time thinking about). Will be updated as I find them…

Get number of duplicate records:

SELECT COUNT(*) FROM table WHERE id NOT IN    (SELECT MAX(id) FROM table GROUP BY col1, col2, col3);

Delete duplicate records:

DELETE FROM table WHERE id NOT IN    (SELECT MAX(id) FROM table GROUP BY col1, col2, col3); 

(Where col1, col2, col3 are the columns you’re looking for duplicates on – from Stack Overflow, tested with SQLite on ~10000 records, took <2s)

Posts navigation

← GNU Screen: Split Screen Terminal
SSH Snippets →
  • Recent Posts

    • Snapshot backups with Btrfs and Snapper – Raspberry Pi
    • Seafile on Raspberry Pi Ubuntu 20.04
    • SSH Snippets
    • SQL Snippets
    • GNU Screen: Split Screen Terminal
  • Recent Comments

    1. Peet on Seafile on Raspberry Pi Ubuntu 20.04
    2. David Somers on GNU Screen: Split Screen Terminal
    3. Alasdair Macintyre on GNU Screen: Split Screen Terminal
    4. David Somers on GNU Screen: Split Screen Terminal
  • Archives

    • July 2020
    • June 2020
    • January 2012
    • December 2011
    • October 2009
  • Categories

    • Uncategorized
Proudly powered by WordPress Theme: Parament by Automattic.