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

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:

Find records from table where changed value of one of columns

For example we have table with customer loan applications, where each customer provide count of child. For simplify task let it be table with 2 columns: name and childs

How to check MS SQL Server version?

Question: What are the possible ways to determine the deployed SQL Server version?