Running the Same Query Against Several Engines
Why one SELECT gives three different answers on PostgreSQL, MySQL and SQLite, the portable version of each query, and how to test it on every database from one window.
On this page
Words in this article
Words in this article
- Engine
- The database software itself, such as PostgreSQL, MySQL or SQLite.
- Dialect
- The version of SQL one engine understands. Most of it is shared, some of it is not.
- NULL
- An empty value: the database does not know it.
- Operator
- A symbol that does something with values, such as
+or||. - Integer division
- Dividing whole numbers and dropping the remainder: 7 / 2 gives 3.
- JDBC driver
- The small Java library that lets a tool talk to one kind of database.
- Connection
- The saved address and login of one database.
- Polyglot persistence
- One application that keeps its data in several kinds of database.
The short answer
An engine is the database software itself, such as PostgreSQL, MySQL or SQLite. The same query can return different results on different engines. We ran one small table on PostgreSQL 17.6, MySQL 8.4.11 and SQLite 3.54 and found three differences:
Joining text
||joins first and last name on PostgreSQL and SQLite. MySQL returns 0 instead.Sorting empty values
A row with an empty value comes last on PostgreSQL, and first on MySQL and SQLite.
Dividing whole numbers
7 / 2gives 3 on PostgreSQL and SQLite, and 3.5000 on MySQL.
What DbSchema adds when you work with several engines
Why DbSchema
DbSchema is a database design tool that connects to each of these engines. Its SQL Editor is the window where you type and run SQL. The rows appear in the result grid under it. The connection button in the toolbar switches between your saved databases. The free Community Edition covers connecting and the SQL Editor, and DbSchema Pro adds saving your design to a file. The connection documentation and the SQL Editor pages explain every field.
Saved connections, one editor
Save one connection per database. The same SQL Editor then runs your query on whichever database is active.
Drivers downloaded for you
DbSchema downloads the JDBC driver, the Java library that talks to the database. DbSchema connects to more than 100 kinds of database.
A diagram of each database
DbSchema reads the tables of a database and draws them as an ER diagram. You see the columns before you query them.
A guard on production
Read Only Connection stops DbSchema from changing a production database. Highlight marks its connection button as Production.
The loop in DbSchema:ConnectRun in the SQL EditorSwitch connectionRun again
The sample table
Every query below runs on this table. It has three customers, and one of them has no city. The empty city is NULL: a missing, unknown value.
CREATE TABLE customers (id INT PRIMARY KEY, first_name VARCHAR(20), last_name VARCHAR(20), city VARCHAR(20));
INSERT INTO customers VALUES (1, 'Ana', 'Lopez', 'Berlin'), (2, 'Ben', 'Novak', NULL), (3, 'Cara', 'Smith', 'Athens');
The same two statements work on all three engines.
Three differences we ran
Each engine understands its own dialect of SQL. A dialect is the version of SQL one engine speaks. Most of it is the same on every engine. The three differences below are where it is not.
Joining text with ||
The || operator joins two pieces of text. An operator is a symbol that does something with values, such as +.
SELECT id, first_name || ' ' || last_name AS full_name FROM customers ORDER BY id;
Result · PostgreSQL 17.6 and SQLite 3.54
| id | full_name |
|---|---|
| 1 | Ana Lopez |
| 2 | Ben Novak |
| 3 | Cara Smith |
Result · MySQL 8.4.11
| id | full_name |
|---|---|
| 1 | 0 |
| 2 | 0 |
| 3 | 0 |
MySQL reads || as logical OR, not as joining text. MySQL then converts each text to a number. Text that does not start with a digit becomes 0, so every full_name is 0. MySQL does not stop with an error. It only adds a warning, a note that the query ran but something looks wrong: "'|| as a synonym for OR' is deprecated". Deprecated means MySQL will remove this meaning of || in a later version. Until then, the wrong names can reach your application.
Sorting empty values
SELECT first_name, city FROM customers ORDER BY city;
Result · PostgreSQL 17.6
| first_name | city |
|---|---|
| Cara | Athens |
| Ana | Berlin |
| Ben | NULL |
Result · MySQL 8.4.11 and SQLite 3.54
| first_name | city |
|---|---|
| Ben | NULL |
| Cara | Athens |
| Ana | Berlin |
PostgreSQL sorts NULL as if it were larger than any other value, so Ben comes last[1]. SQLite treats NULL as smaller than any other value, so Ben comes first[2]. MySQL put Ben first in our run too.
Dividing whole numbers
SELECT 7 / 2 AS result;
Result
| engine | result |
|---|---|
| PostgreSQL 17.6 | 3 |
| SQLite 3.54 | 3 |
| MySQL 8.4.11 | 3.5000 |
PostgreSQL and SQLite do integer division here. Integer division divides whole numbers and drops the part after the decimal point. MySQL keeps the decimals and returns 3.5000.
The common failure and its fix
Each difference has a portable version: a query that works unchanged on every engine. You may also reach for NULLS LAST, which asks for empty values at the end:
SELECT first_name, city FROM customers ORDER BY city NULLS LAST;
PostgreSQL and SQLite run this query. MySQL 8.4.11 rejects it with the second error below. We ran every portable fix on PostgreSQL 17.6, MySQL 8.4.11 and SQLite 3.54.
Why it happens
MySQL reads || as OR.
Portable fix
Use CONCAT(first_name, ' ', last_name). In our run it worked on PostgreSQL, MySQL and SQLite. SQLite lists concat() among its core functions [3].
Why it happens
MySQL has no NULLS LAST. PostgreSQL and SQLite accept it.
Portable fix
Write ORDER BY city IS NULL, city. city IS NULL is false for rows with a city and true for the empty one. False sorts before true, so rows with a city come first, sorted by city.
Why it happens
PostgreSQL and SQLite divide whole numbers as integers.
Portable fix
Make one number a decimal: 7 / 2.0 returns 3.5 on all three engines.
Multi-model database or several engines
A multi-model database is one engine that stores several kinds of data, such as tables and JSON documents. This article is about the other case: one application that uses several engines, which Martin Fowler calls polyglot persistence[4].
Run the same query on each engine in DbSchema
A connection is the saved address and login of one database. DbSchema keeps one per database, and the connection button switches between them.
-
Add a connection for each database Community
Click the connection button in the toolbar and choose Add Connection. Fill in the dialog, press Test Connection, then Connect.
-
Open the SQL Editor
Press Editor in the toolbar and choose SQL Editor, or press Ctrl+E. Paste the query.
-
Run it
Press Run, or Ctrl+Enter. The rows show in the result grid under the editor.
-
Switch the connection and run again
Choose the next database in the connection button, then run the same text. Compare the two result grids.
-
Keep the editor for later Pro
When you close the editor, choose Save and Close. DbSchema keeps the editor in the model: DbSchema's project file that holds your design and your saved SQL editors. Saving the model to a file needs DbSchema Pro. In the Community Edition you can still run every query in this article.
What DbSchema does not let you do to production
When you test on several databases, the production database is often one click away. The Access and Model tab of the connection dialog protects a production connection.
Tick Read Only Connection on a production connection. DbSchema then refuses to change the schema through this connection. The schema is the structure of a database: its tables and their columns. DbSchema also asks the JDBC driver to open the connection read-only. Highlight colors the connection button as Production, Development or Test. You see at a glance which database the next query runs on.
DbSchema connects for free in the Community Edition, including the SQL Editor and Read Only Connection. Download DbSchema, add one connection per database, and run your query on each of them.
Frequently asked questions
Can I run one query on several databases at the same time?
DbSchema runs the query on the active connection. Switching takes one click in the connection button. Run the same query on each database in turn and compare the result grids.
Which SQL works on PostgreSQL, MySQL and SQLite alike?
Plain SELECT, WHERE, JOIN, GROUP BY and ORDER BY work on all three. Watch for text joining, NULL order and division, and test the query on each engine before it ships.
Does DbSchema change my SQL for each engine?
No. The SQL Editor runs exactly the text you typed, so you test what your application will send. Use the portable versions above.
Sources
Test one query on every database
DbSchema keeps one connection per database, downloads the JDBC drivers, and runs your SQL on whichever connection is active. Connecting, the SQL Editor and read-only connections are in the free Community Edition.