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
  • Write
  • About

Groups of groups in SQL (nested GROUP BY)

We’ll show why and how to get what we call “groups of groups”, or aggregations of aggregated data. It means using nested GROUP BY queries.

Continue reading Groups of groups in SQL (nested GROUP BY)
Published May 27, 2022
Categorized as Generic SQL Tagged level-expert

Non-trivial uses of LIKE in SQL

The LIKE operator is generally well-known. Most SQL users know how to find columns that contain a string, start with a string, or end with a string. But some of them, especially beginners, may not find obvious some other interesting uses of LIKE.

Continue reading Non-trivial uses of LIKE in SQL
Published March 10, 2022
Categorized as Generic SQL Tagged level-intermediate

Db2: Loop over an array

We saw in another article how to build an array from an SQL query (from each row, or from a column). This article shows how to loop over an array items.

Continue reading Db2: Loop over an array
Published January 29, 2022
Categorized as Db2 Tagged level-beginner

MariaDB/MySQL: How to get the first and last day of the month

Different databases support different ways to get the first and last day of the month. Let’s see how to do it in MariaDB and MySQL.

Continue reading MariaDB/MySQL: How to get the first and last day of the month
Published January 8, 2022
Categorized as MariaDB, MySQL Tagged level-beginner

Db2: How to create an array from a query

Db2 ordinary arrays can be constructed and populated from an SQL query. Let’s see how to do it.

Continue reading Db2: How to create an array from a query
Published December 12, 2021
Categorized as Db2 Tagged level-intermediate

SQL Server: Find users login time

Finding users who never logins is not trivial in SQL Server. But we can do it by creating a logon trigger.

Continue reading SQL Server: Find users login time
Published December 2, 2021
Categorized as SQL Server Tagged level-intermediate

SQL Server: Writing an HTML encode function

Sometimes we need to extract a text from the database to show it in a web page. The text should be showed as is, including any HTML tags and special characters. Normally the encoding is done by a web application, but there are cases when an SQL string is expected to return HTML encoded texts.

Continue reading SQL Server: Writing an HTML encode function
Published November 28, 2021
Categorized as SQL Server Tagged level-intermediate

How to get the highest and lowest value in a row in SQL

It is well-known how to get the highest and lowest values in a table column, but it is a bit less known how to get the highest and lowest values in a row. Let’s see how to do it.

Continue reading How to get the highest and lowest value in a row in SQL
Published November 21, 2021
Categorized as Generic SQL Tagged level-beginner

How to find the mode in SQL, aka MAX(COUNT(*)) not working

In statistics, the mode is the function that returns the element that appear most often in a series. This is what people try to achieve when they run MAX(COUNT(*))… and they find out that it doesn’t work. Let’s see how to obtain the mode in SQL.

Continue reading How to find the mode in SQL, aka MAX(COUNT(*)) not working
Published November 14, 2021
Categorized as Generic SQL Tagged level-expert, level-intermediate

SQL Server: How to count occurrences of a substring in a string

Sometimes you want to count all occurrences of a substring into a bigger string. For example, you may want to count all occurrences of a name in a text. This can be done, but it’s not straight-forward because SQL Server doesn’t have a specific function to do it.

Continue reading SQL Server: How to count occurrences of a substring in a string
Published November 11, 2021
Categorized as SQL Server Tagged level-intermediate

Posts navigation

Page 1 … Page 7 Older posts
  • Twitter
  • Email
SQL Bits
Proudly sponsored by Vettabase.