Stop 01 · SQL & Database FoundationsTopics · 10/10

SQL Dialects and the ANSI Standard

Part of the SQL Roadmap

In short

You'll hear "SQL" as if it were one thing, then find that a query from a tutorial fails on your database. Every database follows a shared standard and then adds its own accent on top.

SQL has an ANSI/ISO standard, but every database adds its own dialect on top. The core (SELECT, JOIN, GROUP BY) is the same everywhere, while details like row limits, date functions and auto-increment syntax differ.

The jaadu chats happily with three travellers at a railway platform, each wearing a different regional cap.

Think of it like this

Hindi across cities

Someone from Mumbai, Lucknow and Hyderabad all speak Hindi and understand each other. But Mumbai says "bindaas", Hyderabad says "hau", and Lucknow is extra formal. SQL dialects are the same: the core grammar is shared, and each database has its own local words.

Watch it happen

Same question, two dialects

PostgreSQL

  • Close to the ANSI standard
  • Top rows: LIMIT 5 or FETCH FIRST 5 ROWS ONLY
  • Join text with || or CONCAT
  • Auto IDs: GENERATED ALWAYS AS IDENTITY
  • Months: DATE_TRUNC('month', d)

MySQL

  • Adds many of its own shortcuts
  • Top rows: LIMIT 5 only
  • Join text with CONCAT (|| means OR by default)
  • Auto IDs: AUTO_INCREMENT
  • Months: DATE_FORMAT(d, '%Y-%m-01')

SELECT, WHERE, JOIN and GROUP BY are identical. Learn the standard core first, then look up the few dialect differences when you need them.

The big ideas

What you need to know

ANSI/ISO is the shared rulebook

The SQL standard defines the core grammar. No database follows all of it, but all follow most of it.

The core is portable

SELECT, FROM, WHERE, JOIN, GROUP BY, HAVING and ORDER BY behave the same almost everywhere.

Edges differ

Row limits, date functions, string joining, auto-increment and quoting are where dialects disagree most.

Check the docs

When a query errors after switching databases, search "<feature> in MySQL" or "in PostgreSQL" first.

See it in code

Walk through the code

sql
1-- ANSI standard (PostgreSQL, Oracle 12c+, SQL Server with OFFSET 0 ROWS)2SELECT name, price3FROM products4ORDER BY price DESC5FETCH FIRST 3 ROWS ONLY;6 7-- PostgreSQL, MySQL and SQLite shortcut8SELECT name, price FROM products ORDER BY price DESC LIMIT 3;9 10-- SQL Server shortcut11-- SELECT TOP 3 name, price FROM products ORDER BY price DESC;

1/3The standard form. Wordy, but portable to most modern databases.

The three most expensive products, three ways. Both uncommented queries run in PostgreSQL. MySQL supports only the LIMIT version.

Save yourself the pain

Common mistakes

Oops

Using double quotes for text, like WHERE city = "Delhi".

Do this instead

Standard SQL uses single quotes for text. In PostgreSQL, double quotes mean a column or table name.

Oops

Copying a MySQL tutorial query into PostgreSQL and assuming SQL is broken when it fails.

Do this instead

Read the error. It usually points at one dialect-specific function you can swap for its equivalent.

Your mission

Spot the dialect

~20 min

Quick check

Which part of SQL is most likely to differ between PostgreSQL and MySQL?

Go deeper

  1. 1Compare how MySQL (LIMIT), SQL Server (TOP) and the standard (FETCH FIRST n ROWS ONLY) limit rows.
  2. 2Keep a small cheat sheet of syntax differences for the two databases you use most.
  3. 3When searching for help online, always include your database name in the search.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge