Writing sub-queries for complex operations

Subquery is a query statement written within an other query such as SELECT...

Natural sorting with ORDER BY clause

The data in MySQL table can be selected and sorted using SELECT and

How to select random records?

When users have to select a random record from MySQL database, RAND() function may be used. Th...

How to compare and calculate successive rows?

In MySQL database sometimes we want to calculate difference between values in successive rows. Consider an example where the score of a cricket match after every over is stored in another row. The...

Selecting the highest record in a database table

When we need to find the maximum number in a column of our database, the MAX function is used. When we have requirement to get the highest paid employee in a company, the...

Regular expressions in MySQL

Regular expressions, used in most of other programming languages, are also well established in MySQL where they may be used to specify patterns for search operation in a database. The

Importing and exporting CSV to and from MySQL

Data of a table in a database may be dumped (exported) to a file called export database. Similarly, same data may be loaded (exported) from a CSV file called import datab...

Prepared statements in MySQL

Prepared statements are considered to be the immediate statements of MySQL with advantages of client/server binary protocol. Before MySQL 4.1 version, the query that was sent to t...

Listing Stored Procedures in MySQL

In MySQL, stored procedures and their source codes can be viewed for adminsitrational (or other...

How to do basic transactions with MySQL?

In this chapter we will briefly go over basics in transactions, such as money transactions, and how to properly execute them with MySQL. A MySQL transaction is a group of sequential databas...