This article provides a list of SQL operators, and can therefore be used as cheat sheet for SQL operators.
Not all of these operators are supported across all DBMSs. If in doubt, check the documentation for your specific DBMS.
Continue readingThis article provides a list of SQL operators, and can therefore be used as cheat sheet for SQL operators.
Not all of these operators are supported across all DBMSs. If in doubt, check the documentation for your specific DBMS.
Continue readingI get it. You’re trying to learn SQL, but they won’t even tell you the most fundamental part – what you need to run SQL!
You’ve got the SQL code… but where do you actually run it? What software program do you need before you can run your SQL query against a database?
If that’s you, all is about to be revealed!
Continue readingIn 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.
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.
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.
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:
IFNULL()
functionCOALESCE()
functionIF()
function combined with the IS NULL
(or IS NOT NULL
) operatorCASE
expression combined with the IS NULL
(or IS NOT NULL
) operatorExamples of these options are below.
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.
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 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.
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.