How to write a good SQL question?
There are five ingredients to this recipe:
- Provide a clear textual explanation of the problem at hand.
- Provide proper sample data, as DDL (create table statement(s)) and DML (insert statement(s)). The sample data should be enough to demonstrate the problem, including edge cases. Usually, a few relevant rows are enough.
- Provide the expected output for the sample data you've posted.
- Show your attempts to solve the problem.
- Tag properly. In addition to sql, also provide the relevant rdbms tag (i.e. sql-server, oracle, postgresql), and the lowest version you need the solution for (i.e sql-server-2012, oracle10g).
Remember - different products use different dialects of SQL, and this can have a drastic effect on the answers you might get.
Different versions of the same product have a different set of built-in functions and capabilities that might also have a drastic effect on the answers.
Do not include images of data or code!
A link to an online SQL test environment such as SQL Fiddle, Rextester, or DB Fiddle might help, but it is not a replacement for having all the data inside the question.
For more information, Read Why should I provide an MCVE for what seems to me to be a very simple SQL query? and Help me write this query in SQL.
SQL stands for Structured Query Language (informally) and usually pronounced as Sequel.
SQL is based on relational algebra. In relational algebra, the word relation is synonymous with the word table. SQL is a standard to use relational algebra in a technical environment.
One subset of the SQL standard is DDL (Data Definition Language), which is used to create tables and constraints. These include:
Another subset is DML (Data Manipulation Language), which is used to modify and view data within the database:
The final "standard" subset of commands is DCL (Data Control Language):
Many database implementations require the use of SQL, and over the years, vendors have implemented dialects of SQL to provide more functionality as well as simplify it. Because of these deviations from the standard, SQL is fractured – syntax that works on one implementation does not necessarily work on another.
ISO/IEC (formerly ANSI) standards have been beneficial in resolving such situations, but adoption is selective. Queries conforming to these standards should be portable to other databases, though performance may vary.
Most DBMSs have additional languages for writing stored procedures. In Oracle this is PL/SQL (Procedural Language/Structured Query Language), in PostgreSQL it's PL/pgSQL (Procedural Language/PostgreSQL). Outside of stored procedures or functions, Oracle and PostgreSQL use SQL. Thus the tags plsql and plpgsql should only be used for problems directly related to writing stored procedures. Microsoft SQL Server uses the term T-SQL (Transact-SQL)(tsql) for both "plain" SQL (queries, DML, ..) and the language used for stored procedures.
List of Procedural Extensions
- ANSI/ISO Standard: SQL/PSM (SQL / Persistent Stored Modules)
- Interbase/Firebird: PSQL (Procedural SQL)
- IBM DB2 SQL: PL/SQL (Procedural Language, implements SQL/PSM)
- IBM Informix: SPL (Stored Procedural Language)
- IBM Netezza: NZPLSQL (based on Postgres PL/pgSQL)
- Microsoft/Sybase: T-SQL (Transact-SQL)
- Mimer SQL: SQL/PSM (SQL / Persistent Stored Module, implements SQL/PSM)
- MySQL: SQL/PSM (SQL / Persistent Stored Module, implements SQL/PSM)
- MonetDB: SQL/PSM (SQL / Persistent Stored Module, implements SQL/PSM)
- NuoDB: SSP (Starkey Stored Procedures)
- Oracle: PL/SQL (Procedural Language / SQL, based on Ada)
- PostgreSQL: PL/pgSQL (Procedural Language / PostgreSQL Structured Query Language, implements SQL/PSM)
- Sybase: Watcom-SQL (SQL Anywhere Watcom-SQL Dialect)
- Teradata: SPL (Stored Procedural Language)
- SAP: SAP HANA (SQL Script)
This tag should be used for general SQL programming language questions, in addition to tags for specific products. For example, questions about Microsoft SQL Server should use the sql-server tag, while questions regarding MySQL should use the mysql tag. SQL is the umbrella under which these products exist; tagging them by product (including version, e.g oracle11g, sql-server-2008, hana) is the easiest way to know what functionality is available for the task at hand. It is very common for mysql questions to omit this tag because query discussions on MySQL are more often stated as MySQL rather than SQL in general.
More specific tags
When you are asking a question about SQL you can also add more specific tags. Here is the list of available tags:
Implementation specific tags
You can specify your question by adding the implementation you used as a tag.
Free SQL Programming Books
- Developing Time-Oriented Database Applications in SQL
- Use The Index, Luke!: A Guide To SQL Database Performance
- SQL Tutorial For Starters
- SQL - Free books
- SQL - Free books 2
Free SQL/Database Online Courses
SQL/Database Online Tutorial
While you should always provide complete code examples (e.g., schema, data sample and expected result) in your question or answer, you can also isolate problematic code and reproduce it in an online environment:
- SQL Fiddle MySQL 5.6, Oracle 11g R2, PostgreSQL 9.6, PostgreSQL 9.3, SQLite (WebSQL), SQLite (SQL.js), SQL Server 2014
- Rextester: SQL Server, PostgreSQL, MySql, Oracle
- Stack Exchange Data Explorer Microsoft SQL Server 2016
- db-fiddle MySQL 5.5, 5.6, 5.7, 8.0 PostgreSQL 9.4, 9.5, 9.6, 10 SQLite 3.16, 3.17, 3.18
- db <> fiddle MariaDB 10.2, 10.3, MySQL 8.0, Oracle 11.2, Postgres 8.4, 9.4, 9.5, 9.6, 10 and 11, SQLite 3.8, 3.16, SQL Server 2012, 2014, 2016 and 2017