How can I best ask my question so people will be able to understand it and reproduce the problem if necessary?
1 Answer
How to create a Minimal, Complete, and Verifiable Example for database-related questions
Database-related questions asking for practical advice will get the most helpful answers if they provide a framework others can use to reproduce that problem.
With that in mind, when asking a question please create a framework that is:
- Minimal – Use as little code as possible that still produces the same problem
- Complete – Provide all parts needed to reproduce the problem
- Verifiable – Test the code you're about to provide to make sure it reproduces the problem
Minimal
Reducing the code to the bare minimum necessary to convey the problem makes the question easier to ask, and inherently easier to answer. Win-win. If you have a question about a query that has 400 columns, and all 400 columns are not required for the answer, only show the two or three columns that are pertinent to the question.
Complete
Include all the tables, queries, indexes, constraints, and other parts as necessary to ensure the person answering your question has all the information at the outset. When including these pieces, provide the SQL scripts so others don't have to recreate them. Do not provide screenshots of tables or results. You can even use an online resource such as db<>fiddle or SQL Fiddle both of which support most major RDBMS platforms and allow you and others to run your example.
Verifiable
Include test output, in text formatted as a table, to show both what you're currently getting as well as what your desired output should be. Search for "ascii table generator" on your favorite search engine - there are several that are extremely easy to use.
An example question, including an MCVE framework
I want to get the total count of the number of ducks in each pond.
The ponds table:
CREATE TABLE ponds
(
PondName varchar(30)
, DuckName varchar(30)
);
Some sample data:
INSERT INTO ponds (PondName, DuckName)
VALUES ('Golden', 'Daffy')
, ('Walden', 'Daisy');
My query so far:
SELECT COUNT(DuckName)
FROM ponds;
The output I'm getting:
╔═══════╗ ║ Value ║ ╠═══════╣ ║ 2 ║ ╚═══════╝
The output I'd like to get:
╔════════╦═══════╗ ║ Pond ║ Count ║ ╠════════╬═══════╣ ║ Golden ║ 1 ║ ║ Walden ║ 1 ║ ╚════════╩═══════╝
- MySQL: DBFiddle.uk | SQLFiddle
- Oracle: DBFiddle.uk | SQLFiddle
- Postgres: DBFiddle.uk | SQLFiddle
- SQL Server: DBFiddle.uk | SQLFiddle
-
5edits to this answer will occasionally be incorporated in the help centre page by the moderator team– Paul White ModCommented Jul 4, 2019 at 16:38