Prepstellar

SQL Fundamentals · Getting started

18 cards

A SQL Workflow from Connection to Result

Swipe, scroll or use ← →
  1. Start from a database that already exists

    Before any statement runs, a session needs two things: a running PostgreSQL server and a database to work in.

    One PostgreSQL server can manage many databases, and a separate database is commonly used for each project or user. So "connect to PostgreSQL" is never quite enough information — you also say which database.

    Question Typical answer
    Is a server running? Yes, somewhere reachable from your machine.
    How many databases can it hold? Many; one per project or per user is the common arrangement.
    Which one do you want? The one that belongs to the work you are doing.
    1 / 18
  2. Start from a database that already exists

    Here is the part that saves beginners a lot of anxiety: the database may already have been created by an administrator, so using SQL does not always mean administering the server.

    Learning SQL and running a server are two different skills. If someone has provisioned a database for you, you can skip straight to connecting and querying, and come back to server administration later — or never, if that is somebody else's job.

    2 / 18
  3. Quick check

    You want to start practising SQL, but you have never installed or configured a database server. Which statement is accurate?

    1. AAn administrator may already have created the database you need

      Right. Access is often provisioned for you, so learning SQL does not have to begin with server administration.

    2. BEvery SQL user must first stop the running PostgreSQL server

      Stopping the server would remove the very thing that accepts connections and performs the work.

    3. CConnecting to one database turns all the other databases into tables

      A database is not converted into a table by a connection; the hierarchy is unchanged by connecting.

    3 / 18

  4. Pick the client that suits the work

    A database is reached through a client, and there is more than one kind. A PostgreSQL database can be accessed with the psql interactive terminal, a graphical frontend, or a custom application that uses a language binding.

    Client What it is good for
    psql Typing statements and seeing the raw result immediately.
    Graphical frontend Browsing objects and building work through a visual interface.
    Custom application Sending SQL from program code through a language binding.

    They differ in presentation, but each one plays the same client role: it sends a request and shows what comes back. None of them is the database, and none of them is the language.

    4 / 18
  5. Pick the client that suits the work

    For learning, psql gives the clearest visible loop, because nothing sits between what you typed and what the server answered.

    Running psql mydb connects the terminal client to the database named mydb. The argument is the database you want, not a table and not a command.

    Once connected, psql greets you and shows a prompt. That prompt is the signal that the client is listening for your input.

    5 / 18
  6. Quick check

    In the tutorial workflow, what does the command psql mydb do?

    1. AIt creates a new table named psql that has a column called mydb

      Tables are created with a SQL statement, not by naming the client program on the command line.

    2. BIt converts every database on the server into an interactive command

      Databases are not turned into commands; the argument only says which database to work in.

    3. CIt starts the psql terminal client connected to the database named mydb

      Right. The argument names the database that the interactive terminal should access.

    6 / 18

  7. Send a command the server can act on

    The prompt is waiting, so you type. But a client does not fire off every line the moment you press Return — otherwise no statement could ever span two lines.

    SQL commands typed in psql are sent to the server when the command is complete, and a semicolon terminates the command.

    That single rule explains the most common first surprise. Type SELECT current_date and press Return, and nothing happens: the client is still waiting, because the command is not finished. Add the semicolon and it goes.

    7 / 18
  8. Send a command the server can act on

    Once the command is complete, the server performs the requested database action on behalf of the client and answers.

    What comes back What it means
    Rows The query found data and is showing it to you.
    A confirmation A change was accepted; the stored state is now different.
    An error The request could not be carried out, and the message guides the next revision.

    An error is information, not a failure of the session. It usually names what the server could not understand, which is exactly what you need in order to fix the statement and send it again.

    8 / 18
  9. Quick check

    You type a SELECT statement in psql, press Return, and nothing is sent. What is the most likely reason?

    1. AThe client only accepts statements that were written in a graphical tool

      psql is a text client that accepts typed SQL; it does not require another tool to write it.

    2. BThe command has not been terminated, so the semicolon is still missing

      Right. The command is sent only when it is complete, and the semicolon is what terminates it.

    3. CThe database was closed by the server as soon as the prompt appeared

      The prompt is the sign that the client is listening; it does not mean the database has been closed.

    9 / 18

  10. Keep your progress in the app

    That’s 3 of 7 quick checks. In the app they stay answered, and every lesson remembers where you left off.

  11. Backslash commands talk to the client, not to the database

    Some things you type in psql never reach the server at all, and confusing the two categories leads to puzzling errors.

    Commands internal to psql begin with a backslash and are not SQL commands. For example, \h requests syntax help and \q exits psql.

    What you type Who handles it What happens
    \h The client Shows syntax help for SQL commands.
    \q The client Leaves psql and returns you to the shell.
    SELECT current_date; The server Evaluates the SQL and returns a result.

    A backslash command controls your work surface; a statement asks the database system to do database work. Asking for help is not a data request, and a data request cannot be made by leaving the client.

    10 / 18
  12. Quick check

    How do psql's own internal commands differ from SQL sent to the server?

    1. AThey are written as a table row rather than as a statement

      A table row is stored data; it is not a way of typing a command at the prompt.

    2. BThey begin with a backslash and are handled by the client

      Right. The backslash prefix marks a command that psql handles itself instead of sending it as SQL.

    3. CThey can only be typed after the connection has been closed

      These commands are used during a session, at the same prompt where SQL is typed.

    11 / 18

  13. Build it, fill it, look at it

    The first end-to-end loop is deliberately small: one table, a few rows, one query. Small is the point, because you can see every effect.

    A table is created by naming the table and specifying its columns and data types. That single statement fixes the shape of the data before any value exists.

    Then INSERT populates a table with rows, while SELECT retrieves data from a table. The query result is your evidence: it shows whether the structure and the stored values actually answer the question you had in mind.

    12 / 18
  14. Build it, fill it, look at it

    Reality moves, so the loop continues. UPDATE changes existing rows and DELETE removes rows.

    Need Statement
    Decide the shape of the data CREATE TABLE, naming columns and their types
    Put the first records in INSERT
    See what is stored SELECT
    Correct a value that changed UPDATE
    Remove what no longer belongs DELETE

    A project that needs repeatable storage, a first set of records, and visible proof that retrieval works follows the first three steps in order, then keeps the last two for later corrections.

    13 / 18
  15. Quick check

    A small project needs repeatable storage, a first set of records, and visible evidence that retrieval works. Which path meets all three needs?

    1. ADefine the table, insert the records, then run a SELECT and read its result

      Right. The table and the inserted rows create the stored state, and the query returns observable evidence.

    2. BExit psql, drop the database, and treat the missing result as a success

      Removing the database destroys the very state the project needs, and an absent result proves nothing.

    3. CUse a backslash command as the table, then read its help text as stored rows

      A backslash command is client help; it stores no data and returns no rows from the database.

    14 / 18

  16. Inspect, revise, and keep what is already stored

    This is an iterative workflow rather than a single perfect command: connect to the intended database, enter a complete statement, inspect the returned result or message, refine the statement, and repeat.

    Suppose a user working through a graphical frontend gets a result that is not what they expected, and the stored data must be preserved. The answer is to stay in the loop: read the result, revise the SQL in the same client role, and send another complete command. Nothing about an unexpected result calls for removing the server or converting the statement into something else.

    The database holds managed state across commands, while the client provides the work surface for the session. Later lessons add joins, aggregation, transactions, and performance decisions to this same basic loop.

    15 / 18
  17. Quick check

    A graphical-frontend user gets an unexpected query result and must keep the stored data while refining the request. What comes next?

    1. AReplace the graphical client with a table and treat the result as its data type

      A client is a program and a table is stored data; neither can take the other's place in the session.

    2. BRemove the PostgreSQL server and its databases because one result was wrong

      One unsatisfactory result is a reason to revise the statement, not to destroy the managed state.

    3. CInspect the result, revise the SQL, and send another complete command

      Right. Send, inspect, revise, repeat is the loop, and it works from any client without discarding stored data.

    16 / 18

  18. Key takeaways

    • Select the intended database and connect through a client such as psql — a graphical frontend or a custom application plays the same role, and the database may already have been created for you.
    • psql mydb opens the terminal client on the database mydb, and the prompt means the client is listening.
    • Send complete SQL commands to the server and inspect the returned rows, confirmation, or error; the semicolon is what completes the command.
    • Backslash commands such as \h and \q are handled by psql itself and are not SQL sent to the server.
    • Use the recurring define, populate, query, and modify loop to develop database work, refining statements while the database keeps its state.
    17 / 18
  19. Quick check

    Which summary keeps the connection, the client, and the SQL in their right places?

    1. AChoose the intended database, connect through a client, and end each statement so that the server can act on it

      Right. The database is chosen, a client carries the request, and a completed command is what the server executes.

    2. BOpen any client, and every typed line reaches the server as SQL at once

      Backslash commands never reach the server, and an unterminated statement is not sent at all.

    3. CStart with a backslash command as the query, and let the client store the returned rows

      Backslash commands control the client rather than query data, and the database holds the stored rows.

    18 / 18

  20. 7 quick checks · then the test

    In the app, finishing the quick checks opens this lesson’s 10-question test, and the ones you miss come back exactly when you’re about to forget them.

The whole course, on your phone

Lessons you can read, audio you can listen to on the way to work, and practice that remembers what you got wrong.