Skip to content

SQL Bits

You're here to find quick SQL recipes.

  • Home
  • Topics
    • Generic SQL
    • PostgreSQL
    • MariaDB
    • MySQL
    • Oracle
    • SQL Server
    • Db2
    • Informix
    • SQLite
  • Advertising
  • About

Category: Generic SQL

Articles about standard SQL features that will work on the most common DBMSs, and articles that include information about several DBMSs.

Select values that occur multiple times

There are many cases when we want to select values that occur multiple times: customers that ordered the same product multiple times, users with the same email, and so on. We can do this with GROUP BY and HAVING.

Continue reading Select values that occur multiple times
Published August 25, 2021
Categorized as Generic SQL Tagged level-intermediate

Return missing combinations of items from different tables

How to find out which combinations of items from different tables never appear in a third table.

Continue reading Return missing combinations of items from different tables
Published August 14, 2021
Categorized as Generic SQL Tagged level-expert

Check if more than N rows are returned

Counting the rows that match a WHERE condition can be slow, and may return a very high and precise number that is useful to no one. Let’s see how to just check if a query returns more than N rows.

Continue reading Check if more than N rows are returned
Published August 11, 2021
Categorized as Generic SQL Tagged level-intermediate

Comments on database objects

Why and how to write comments on database, tables, and other objects on most DBMSs. How to read comments.

Continue reading Comments on database objects
Published August 9, 2021
Categorized as Db2, Generic SQL, Oracle, PostgreSQL Tagged level-intermediate

Make searches at the end of a text fast

Searching for a string at the end of a text column is typically slow, because no index is used. How to build a suitable index and make LIKE '%end' fast.

Continue reading Make searches at the end of a text fast
Published July 28, 2021
Categorized as Generic SQL Tagged level-intermediate

Find texts starting, ending or containing a given string

How to select rows where a text column starts by, ends by, or contains a string. Including slightly less trivial cases.

Continue reading Find texts starting, ending or containing a given string
Published July 28, 2021
Categorized as Generic SQL, PostgreSQL Tagged level-beginner

Aggregate data that match a condition

How to aggregate data that match a given condition. How to perform different aggregations of data that match different conditions, in a single query. Includes convenient SQLite shortcuts.

Continue reading Aggregate data that match a condition
Published July 27, 2021
Categorized as Generic SQL, SQLite Tagged level-intermediate

Find texts that contain HTML tags

How to select text values that contain HTML tags. How to disallow inserting texts that contain HTML tags.

Continue reading Find texts that contain HTML tags
Published July 26, 2021
Categorized as Generic SQL Tagged level-beginner

Get distinct values that are stored in multiple columns

Getting a list of unique values from a column is easy. But if the same type of values are written on multiple columns and we want to eliminate duplicates, this requires a little trick.

Continue reading Get distinct values that are stored in multiple columns
Published July 26, 2021
Categorized as Generic SQL Tagged level-intermediate

Find rows that (don’t) contain a value from a list

How to find rows that contain, or don’t contain, a value from a list. The list may be written in the query, or stored in a table.

Continue reading Find rows that (don’t) contain a value from a list
Published July 25, 2021
Categorized as Generic SQL Tagged level-beginner

Posts navigation

Newer posts Page 1 Page 2 Page 3 Older posts
  • Twitter
  • Email
SQL Bits
Proudly sponsored by Vettabase.