Etiquette for Asking Your First SQL Question Online

Asking for help is a skill, and it is a skill that gets you better answers faster. This guide explains how to ask your first SQL question on a forum, Q&A site or club chat in a way that makes people want to help.

Before you ask

  1. Read the error message carefully. Copy it exactly. Often it names the problem, such as "no such column: nmae".
  2. Search for the message. Many beginner errors have been asked and answered before.
  3. Try to make it smaller. Remove parts of the query until you find the smallest version that still goes wrong.

What to include

A good question has five parts:

PartExample
What you are trying to do"List each student with the number of courses they take"
Your table definitionsThe CREATE TABLE statements
A few sample rowsThree or four INSERT lines
Your queryThe exact SQL you ran
What happened vs. what you expected"I get an error / I expected 3 rows and got 9"

Also say which database you use (SQLite, PostgreSQL, MySQL) and, if you know it, the version.

A good example

I'm using SQLite. I want each student with how many courses they take.

CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE enrolments (student_id INTEGER, course_id INTEGER);
INSERT INTO students VALUES (1,'Ana'),(2,'Ben');
INSERT INTO enrolments VALUES (1,10),(1,11);
SELECT s.name, COUNT(*) FROM students s JOIN enrolments e ON e.student_id = s.id GROUP BY s.name;

I expected Ben with 0, but Ben is missing. Why?

Anyone can copy that into SQLite and see the problem straight away. (The answer: use LEFT JOIN and COUNT(e.course_id).)

Homework and assignments

It is fine to ask for help with coursework, but be honest that it is an assignment, show what you tried, and ask for an explanation rather than a finished answer. Check your school's rules about getting outside help. You will learn more, and people are much happier to help someone who is trying.

Tone

Protect yourself and others

Never post passwords, connection strings with real passwords, or real personal data such as classmates' names and emails. Replace them with made-up examples.

After you get an answer

Paying it forward

Once you have learned something, look for a question you can answer. Explaining an idea to someone else is one of the best ways to understand it properly. Our page on finding a coding club or study group lists places where you can do that.

A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.