SQL Server CASE Expression

In SQL Server, the T-SQL CASE expression is a scalar expression that returns a value based on conditional logic. It evaluates a list of conditions and returns a value, based on the outcome of those conditions..

In some ways, the SQL Server CASE expression is similar to IF...ELSE. However, CASE allows you to check for multiple conditions, whereas IF...ELSE doesn’t.

Continue reading

How to Write an ORDER BY Clause with Exceptions using SQL

In SQL, the ORDER BY clause is commonly used to order the results of a query. It allows you to select one or more columns to order the results, and in most cases, it’s probably all you need.

But what if you need to make an exception?

What if you want the results to be ordered alphabetically, except for one row? Or several rows?

Or perhaps you simply want to put any NULL values to the end, while ordering the non-NULL results.

Either way, there’s a neat trick you can use that will enable you to do this. And the good part is, it’s simple.

You can cater for all of the above scenarios by adding a CASE expression to your ORDER BY clause.

Continue reading

What is the Year 2038 Problem?

The Year 2038 problem (also referred to as the Y2K38 bug) refers to a problem that some computer systems might encounter when dealing with times past 2038-01-19 03:14:07.

Many computer systems, such as Unix and Unix-based systems, don’t calculate time using the Gregorian calendar. They calculate time as the number of seconds since 1 January 1970. Therefore, in these systems, time is represented as a big number (i.e. the number of seconds passed since 1970-01-01 00:00:00). This is typically referred to as Epoch time, Unix time, Unix Epoch time, or POSIX time. As I write this, Unix time is 1560913841. And as I write this next line, Unix time has incremented to 1560913879.

Continue reading

4 Ways to Replace NULL with a Different Value in MySQL

In MySQL, sometimes you don’t want NULL values to be returned as NULL. Sometimes you want NULL values to be returned with a different value, such as “N/A”, “Not Applicable”, “None”, or even the empty string “”.

Fortunately there are several ways to do this in MySQL.

Here are four:

  • The IFNULL() function
  • The COALESCE() function
  • The IF() function combined with the IS NULL (or IS NOT NULL) operator
  • The CASE expression combined with the IS NULL (or IS NOT NULL) operator

Examples of these options are below.

Continue reading

MySQL Group_Concat() vs T-SQL String_Agg()

One of the T-SQL functions introduced in SQL Server 2017 is the STRING_AGG() function. This is basically the equivalent of MySQL’s GROUP_CONCAT() function – it lets you return query results as a delimited list, rather than in rows.

But there are a few minor differences between the two functions.

This article explores some of the main syntax differences between these functions.

Continue reading

How to Return Query Results as a Comma Separated List in MySQL

In MySQL, you can return your query results as a comma separated list by using the GROUP_CONCAT() function.

The GROUP_CONCAT() function was built specifically for the purpose of concatenating a query’s result set into a list separated by either a comma, or a delimiter of your choice.

This article provides examples of how it all works.
Continue reading

MySQL COUNT() – Get the Number of Rows to be Returned by a Query

MySQL includes a COUNT() function, which allows you to find out how many rows would be returned from a query. This function is part of the SQL standard, and it can be used with most relational database management systems.

The COUNT() function can also be used to affect the results of a query if required, for example, by only returning those results that have a row count greater than a given amount.

This article contains examples of COUNT() usage in MySQL.

Continue reading

How to Check the Size of All Tables within a Database in MySQL

In MySQL, you can check the size of all tables within a given database (or on all databases) by querying theĀ information_schema.tables table. This table stores data about each table in a database, including information about each table’s size, creation date, collation, etc.

You can also find the size of each table within a database by using the MySQL Workbench GUI.

This article provides a quick overview of each method.

Continue reading

How to Check the Size of a Database in MySQL

In MySQL, you can query the information_schema.tables table to return information about the tables in a database. This table includes information about the data length, index length, as well as other details such as collation, creation time, etc. You can use the information in this table to find the size of a given database or all databases on the server.

You can also use the MySQL Workbench GUI to find details about the database (including its size).

This article provides a quick overview of both methods.

Continue reading

AVG() – Calculate the Average Value of a Column in MySQL

When using MySQL, you can use the AVG() function to calculate the average value from a range of values.

For example, you can use this function to find out what the average city population is for a given country or state. Given a country will have many cities, each with different populations, you can find out what the average is between them. One city might have a population of say, 50,000 while another has a population of 500,000. The AVG() function will calculate the average for you.

Continue reading