Introduction to SQL Syntax

What is SQL?

The Language of Data

SQL (Structured Query Language) is the standard way we talk to relational databases. Unlike languages where you describe how to do something, SQL is declarative: you describe what you want, and the database handles the rest.

Welcome to the world of databases, where we'll learn to speak the language of data. SQL, or Structured Query Language, is how we communicate with systems like our Movie database. Think of it as a declarative language; instead of giving a recipe, you are simply ordering from a menu. You tell the database 'what' data you want, and it figures out the most efficient way to get it for you.

What is SQL?

The Language of Data

SQL (Structured Query Language) is a declarative language. This means you tell the database what you want, not how to get it. It acts as a bridge between you and the data stored in tables like Movie.

Welcome to the world of SQL. Unlike traditional programming where you give step-by-step instructions, SQL is declarative. You simply describe what data you need, and the database engine handles the heavy lifting of finding it. In other languages, you might write a complex loop to find a movie. This is the 'How'. In SQL, you just say 'Give me the titles'. This is the 'What'. It's much more efficient for data analysis.

High-Level Syntax Rules

The Rules of the Road

To write clean, professional SQL, follow these core conventions:

Writing SQL is like writing a letter; there are certain rules of etiquette to follow. First, we write keywords like SELECT and FROM in uppercase to make them stand out from our data. Next, we end our instructions with a semicolon to tell the database we are finished. Finally, remember that SQL ignores extra spaces, so feel free to use new lines to keep your code readable for humans!

Core Syntax Rules

The SQL Grammar

To communicate effectively, you must follow specific rules. SQL uses Keywords for commands and Semicolons to end thoughts.

Let's look at the basic rules that keep your queries clean. First, notice the keywords like SELECT and FROM. We write these in UPPERCASE to make them stand out from table names. The semicolon at the end is like a period in a sentence. It tells the database the instruction is complete. SQL doesn't care about extra spaces or new lines. Use them to make your code readable for yourself and others.

Logical Execution Order

Written vs. Logical Order

While we write SELECT first, the database processes the query in a specific logical order.

  1. FROM: Where is the data?
  2. WHERE: Which rows do I keep?
  3. SELECT: Which columns do I show?

It might surprise you, but the database doesn't read your query from top to bottom. It starts with FROM, because it first needs to know which table to open. Then, it looks at the WHERE clause to filter out any data that doesn't fit your criteria. Only at the very end does it perform the SELECT to pick the specific columns you asked for.

Anatomy: Writing vs. Execution

How the Database Thinks

While we write SELECT first, the database actually looks at the FROM clause first to find the table.

This is a common point of confusion for beginners. We write queries in one order, but the database executes them in another. We write SELECT first to define our columns. However, the database engine starts with FROM. It has to identify the table—like the Movie table—before it can possibly know which columns are available to show you.

Exploring the Movie Schema

The Movie Table

Before writing queries, you must know your schema. Our Movie table contains specific columns that must be spelled exactly right.

Let's look at the blueprint of our data, also known as a schema. Our Movie table has five columns: ID, Title, Genre, RatingCode, and Year. When you write a query, you must spell these names exactly as they appear here. While SELECT is case-insensitive, the actual movie titles stored in the database are not!

Practical Example: The Movie Table

Retrieving Data

Let's look at the Movie table. It contains columns like Title, Genre, and Year.

Here is our Movie table. To see a list of all titles, we write a simple query. And the FROM keyword points to the Movie table. When run, the database returns just that single column of data. The SELECT keyword points to the Title column.

Exercise: Complete the Query

Your Turn

Retrieve the Genre and Year for every entry in the Movie table. Drag the correct keywords into the blanks.

Let's practice. We want to see the Genre and Year of our movies. Drag the keywords into the correct slots to complete the instruction. Excellent! You used SELECT to define the columns and FROM to define the table. Notice the comma between Genre and Year—that's essential for multiple columns.

Practice: Your First Query

Fill in the Blanks

Complete the query to retrieve the Genre and Year from the Movie table.

_______ Genre, Year _______ Movie;

Now it's your turn to be the architect. Complete the query by filling in the missing keywords. Remember what we use to choose columns and what we use to identify the table. Excellent! You used SELECT for the columns and FROM for the table. Notice the comma between Genre and Year!

Spot the Syntax Errors

Debugging Challenge

This query has two errors. Can you find and click on them?

SELECT Title Genre FROM Movies;

Even pros make mistakes! This query won't run. Look closely at the columns and the table name, and click on the two areas that are causing the problem. Great catch! You need a comma to separate Title and Genre. Exactly! Our table is named Movie, singular. Adding an 's' will confuse the database. Perfect. With the comma added and the table name corrected, your query is ready to execute.

Spot the Syntax Errors

Debugging Practice

Databases are very literal. A single missing comma or a spelling mistake will cause an error. Click on the three errors in the query below.

This query is broken. It's trying to get the Title and Genre from the Movie table. Can you find and click on the three syntax errors? Great job! You found the missing comma, the misspelled table name, and the missing semicolon. Mastering these small details prevents hours of frustration later.