Prepstellar

SQL Fundamentals · Getting started

14 cards

SQL and Relational Databases

Swipe, scroll or use ← →
  1. Ask a table a question

    SQL is the language you use to work with data that is kept in tables. You write a short statement that says what you want, and the database sends the rows back.

    This is the artists table of the small music store used all through the course:

    artist_id name
    1 AC/DC
    2 Accept
    3 Aerosmith
    4 Alanis Morissette

    Ask it for the name of artist number two:

    SELECT name
    FROM artists
    WHERE artist_id = 2;
    

    The answer arrives as a small table of its own:

    name
    Accept

    The database looks in the artists table, keeps only the row whose artist_id is the number you asked for, and returns the name of that row. You said what you wanted, not how to find it. That is the whole idea of SQL.

    1 / 14
  2. Quick check

    Send the same statement again, but asking for artist number three. What comes back?

    1. AOne row, with Aerosmith

      Right. The row with artist_id three holds Aerosmith, and the select list asks only for the name.

    2. BThree rows, for artists one, two and three

      The condition keeps a single row, so the other artists stay out of the answer.

    3. COne row, with the number three

      The select list asks for the name column, so the answer holds the name and not the number.

    2 / 14

  3. Keep each fact in one place

    A database is a structured collection of data. To add, read and change that data you need a database system, and SQL is how you ask it for things.

    A relational database keeps data in separate tables instead of one big store. An album row does not repeat the artist name: it keeps the artist number, so a filter on albums uses artist_id.

    album_id title artist_id
    1 For Those About To Rock 1
    2 Balls to the Wall 2
    3 Restless and Wild 2
    4 Let There Be Rock 1
    SELECT title
    FROM albums
    WHERE artist_id = 2;
    
    title
    Balls to the Wall
    Restless and Wild

    Two album rows carry the number two, so two titles come back. With WHERE artist_id = 1 instead, the first and fourth album rows match, so the query returns For Those About To Rock and Let There Be Rock. Because the name is stored once in artists, an artist that changes its name is edited in one row only. The database also enforces the rules you set between tables, such as a foreign key from albums to artists. A foreign key does not stop duplicate rows; that needs a unique constraint. For example, with ON DELETE RESTRICT, it stops you from deleting artist two while albums still point to it. With ON DELETE CASCADE, it deletes those albums too. It also refuses a new album whose artist_id matches no artist.

    3 / 14
  4. Quick check

    Nothing else changes, but the third album is moved to artist four. Which titles does the same statement return now?

    1. ABalls to the Wall, Restless and Wild

      Only rows whose artist_id is two are kept, and the third album no longer carries that number.

    2. BBalls to the Wall only

      Right. Only the second album row still carries artist_id two.

    3. CRestless and Wild only

      Restless and Wild now carries artist_id four, so the condition leaves it out.

    4 / 14

  5. Tell the language from the product

    SQL is the language you write; the database system is the product that keeps the data and carries out your statement. People use the word database for both, and that is where the confusion starts.

    The name What it means
    SQL the language, a piece of text you write
    A database system the product that stores the data and runs that text

    The statement you read a moment ago is only text. On its own it stores nothing and finds nothing: some product has to receive it, look in the tables and answer with rows.

    5 / 14
  6. Quick check

    A team installs a product to hold its sales tables, and writes short statements to ask for last month's rows. Which reading is correct?

    1. AThe statements are the product, and the installed software is the language

      A statement is text that is sent to the product; it does not store the tables.

    2. BThe installed product and the statements are two names for one thing

      One is a language and the other is software that runs it, so they are not the same thing.

    3. CThe installed product is the database system, and the statements are SQL

      Right. The software keeps the data, and SQL is what the team writes to ask for it.

    6 / 14

  7. 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.

    Opens in your browser. Nothing to install.

    Start studying

    On your phone, with the same account

    Get it on Google Play Download on the App Store
    Keep reading here
  8. Meet four products this course refers to

    This course refers to four products by name, so it is worth putting a face to each of them. Each one is described here with the words of its own documentation:

    Product How its own documentation describes it
    PostgreSQL an open source object-relational database system that uses and extends the SQL language
    MySQL an open source SQL database management system whose databases are relational
    SQL Server a relational database management system that applications and tools connect to
    SQLite a self-contained, serverless SQL database engine that reads and writes ordinary disk files

    SQL Server is a relational database management system, and the other three are database systems too: none of the four is the SQL language itself.

    Most SQL databases run as a server that your program connects to, and applications and tools reach SQL Server that way. SQLite is different: it has no separate server process, and a whole database with its tables lives in a single ordinary file.

    A server database runs as a separate process reached by a client, while SQLite runs inside your app and reads a local file.CLIENT APPconnectionreads and writesClient appDatabaseserverSQLite engineOrdinary file
    Most of these products use a separate server process. A client can connect locally or over a network. SQLite has no server: the engine runs inside your program, and the whole database is one ordinary file.

    This course does not pick one of them for you, and none of them is needed to follow the lessons. What you learn here is the language they share.

    7 / 14
  9. Quick check

    A small program has to carry its data inside one ordinary file, and nobody wants to install and run a server for it. Which description on the slide fits that need?

    1. AThe object-relational system that uses and extends the SQL language

      This description says nothing about a single file, nor about working without a server.

    2. BThe system that applications and tools connect to

      Applications and tools connect to it, so there is a server to install and reach.

    3. CThe engine that reads and writes ordinary disk files

      Right. That is the one with no separate server process and a database in a single file.

    8 / 14

  10. Run the same query anywhere

    The four products have a great deal in common. All of them keep data in tables of rows and columns, and all of them read SQL.

    SQL is a standardized language, and each of the four products implements it, so a plain statement can be sent to any of them. PostgreSQL, for one, states that it conforms to at least 170 of the 177 mandatory features of the core standard.

    track_id name unit_price
    1 For Those About To Rock 0.99
    3 Fast As a Shark 0.99
    8 Walk On Water 1.29
    9 All I Really Want 1.29
    SELECT name, unit_price
    FROM tracks
    WHERE unit_price > 0.99
    ORDER BY name;
    
    name unit_price
    All I Really Want 1.29
    Walk On Water 1.29

    Two tracks cost more than the others, so two rows come back. The rows come back sorted by name, so All I Really Want is printed before Walk On Water. Every word of that statement is plain SQL, which is why the core of the language is worth learning once.

    9 / 14
  11. Quick check

    The last line is removed, so the statement no longer sorts the rows. What can you still rely on?

    1. AThe two track names are returned in the same order as before

      Without a sorting line the order is not promised, so the earlier order is not guaranteed.

    2. BThe same two rows are returned, in an order the database is free to choose

      Right. The condition still keeps the same two tracks; only their order stops being fixed.

    3. CThe rows are returned in the order in which they were priced

      Nothing in the statement mentions prices as an order, so this is not promised either.

    10 / 14

  12. Expect some differences

    The products are not identical, and their own pages say so. Each product documents extra data types and features of its own, so a script that uses them needs checking before it moves to another product. Applications talk to SQL Server in a dialect of its own called Transact-SQL, and SQLite, unlike most other SQL databases, works without a separate server process.

    PostgreSQL also states that no relational database meets full conformance with the SQL standard, which is a fair warning for all of them.

    Learn the shared core, which is what this course teaches, and when a statement looks unusual, or when you move it to another product, check the documentation of the product you are using.

    11 / 14
  13. Quick check

    A statement you were sent uses a data type that you cannot find in the documentation of the product your team runs. What is the reasonable next move?

    1. ALook in that documentation for a data type that does the same job

      Right. The documentation of the product you are using settles what it offers and what to write instead.

    2. BSend the statement as it is, because the four products read SQL

      A type that is documented as an addition of another product is exactly what yours may not have.

    3. CDrop the statement, since nothing written elsewhere can be reused

      Plain SQL does move between products; it is the extra parts that need a second look.

    12 / 14

  14. Key takeaways

    SQL is the language you write; the database system is the product that keeps the data and carries out your statement. You say what you want, and the rows come back.

    A well-designed relational database keeps each fact in one place, in separate tables, and enforces the rules you define between them.

    PostgreSQL, MySQL, SQL Server and SQLite are four database systems, not four languages. They store data in tables and they read SQL, so the core you learn here travels. The parts that a product documents as its own are the parts to check before you move a statement.

    13 / 14
  15. Quick check

    You have written a plain SELECT statement at home and your new team uses a different product. What is the reasonable first assumption?

    1. AIt has to be rewritten, because every product has its own language

      The four products read SQL; only the parts documented as product-specific need rewriting.

    2. BIt should run, because a plain statement uses the language all four share

      Right. The shared core is what the standard covers, and that is what you wrote.

    3. CIt will only run once the two products are installed side by side

      One product is enough to run the statement; nothing has to be installed beside it.

    14 / 14

  16. 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.

    Take this lesson’s test → Next lesson: Database Structure and Core Vocabulary

    Opens in your browser. Nothing to install.

    Start studying

    On your phone, with the same account

    Get it on Google Play Download on the App Store

The whole course, on your computer and your phone

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

Opens in your browser. Nothing to install.

Start studying

On your phone, with the same account

Get it on Google Play Download on the App Store

“This app is the ‘One Lake’ for preparing the certification exams. Really worth it.”

Abdelhak Y. Google Play review