@sqlize
Most interesting SQL snippets from SQLize.online with comments
2 Followers
0 Following
16 posts
SQL tricks

FETCH FIRST … ROWS WITH TIES

В этой статье я хочу показать новще решение распостраненной задачи. Найти в таблице записи с максимальным значением в одной из колонок.
Для примера мы будем искать сотрудников с максимальной зарплатой.

MySQL 8: short syntax for select all

Since MySQL 8 you can use pretty short query to select all data from table.
Just use TABLE a; instead SELECT * FROM a; and get same result

How get most represented record in table

On Sakila Database we have table category represented categories of movies in DB and table film_category where each film linked it it's category (one film can be present in more then one category).

MySQL: How to re-index primary key numeration

The simplest way to fix order in primary key column is drop it and create again. Assume we have the test where id is column values not in order

Find items presents in each category in many-to-many relation

Assume we have 3 tables items, categories and item_categories where stored records about items, categories and relations between them

How to find anomaly in sequence

Assume we have table with integer column. How we can find is all values of this column are sequence without gaps or duplicates.

Calculate transactions rolling balance with max limit

One more case using CTE (common table expressions). Assume we have table of financial transactions like posted below, where Id is transaction identifier integer auto increment without gaps, Type is D for deposits and W for withdrawals

How to find records where text field does not contains digits?

MySQL solution (using rlike):

How to calculate average value from json array (PostgreSQL)

Assume we have table stored values within json array. For example:

How to remove duplicates from one column table?

Let we have some one table contains duplicates values: