CodeGym /Courses /SQL SELF /Core SQL Operations: Why Do We Need SELECT

Core SQL Operations: Why Do We Need SELECT

SQL SELF
Level 1 , Lesson 3
Available

Before we dive headfirst into practice, let’s figure out what SQL is (you can say “ess-cue-ell” or “sequel”, whichever you like) and why it matters.

SQL stands for Structured Query Language. It’s the standard language used to interact with databases. No matter which DBMS you pick—PostgreSQL, MySQL, Oracle, or whatever—you’ll almost always use SQL.

Think of SQL as your way to “talk” to a database. You give it commands like “show me all students older than 18”, and it’s like a magic wand that pulls out the data you want.

Main things you do with SQL:

  • Fetching data – find out who signed up for the “History of the Middle Ages” course.
  • Adding data – add a new student to the database.
  • Updating data – fix a typo in a student’s name.
  • Deleting data – remove a student who dropped out of college.

Why do we need SELECT?

SELECT is probably the most important command in SQL. In English, it literally means “choose”. It’s your tool for pulling out the info you need from a database.

Imagine you walk into a library with thousands of books. You want to find a book about programming in Python. Instead of wandering the shelves, you tell the librarian: “Bring me books about Python.”

In the database world, SQL is your librarian, and SELECT is the “Bring me…” command.

Here are some things you can do with SELECT:

  • Get the names of all students.
  • Find out how many courses are available this semester.
  • Find students who scored above 90 points.

Besides fetching data, SELECT lets you:

  • Filter data – “show only students older than 18”.
  • Sort data – “sort students by last name”.
  • Show only the columns you need, not everything at once.

Why SQL is awesome

SQL is a skill that’s valued pretty much everywhere.

Want to analyze sales data in an online store? You need SQL.

Working on medical research? Again, SQL. Automating stuff? You guessed it. SQL.

Why is SQL so cool?

  • SQL works with almost any database.
  • SQL is written to be super intuitive.
  • Even complex SQL queries are pretty easy to understand.
  • With SQL, you can handle huge amounts of data and pull out exactly what you need.

Comments in PostgreSQL

And one more important thing—comments in SQL queries. Comments are super useful. First, you can write explanations or your ideas in them. Second, you can use comments to turn off code that doesn’t work. Or comment out an old version of your code.

I’ll be writing comments next to the code a lot, so that’s actually where we’ll start today.

In SQL, just like in many programming languages, there are single-line and multi-line comments. A multi-line comment looks like this:

/* comment text comment text comment text */ 

It starts with /* and ends with */. You can use it on a single line too. Example:

 /* comment text */

There’s also a comment type “from the start to the end of the line”, just like Java’s “//” or Python’s “#”. But in SQL, you need to write two minus signs and a space. Here’s what it looks like:

-- comment text

Here’s an example of a long SQL query with the right side commented out:

SELECT * FROM employee -- WHERE YEAR(join_date) = 2015 

In the example above, we commented out the query condition, so PostgreSQL will only run the query:

SELECT * FROM employee

I’ll write my comments next to the code to make it easier to read or to break down tricky stuff. I’m already doing it! And you should totally use this handy tool too. :)

SQL Query Style

SQL was designed to be as user-friendly as possible.

First, the case of your query text usually doesn’t matter: you can write SELECT, Select, SeLecT, or select, and it’ll all work. Second, line breaks are generally ignored. The DBMS can process your query as if it was written in one long line. So you’ve got some freedom in how you write your queries.

Still, well-formatted SQL code is way easier to read, understand, debug, and maintain. That’s why there are some common style recommendations.

Formatting tips

  • SQL keywords in UPPERCASE. Words like SELECT, FROM, WHERE, ORDER BY, etc., are usually written in all caps. This helps you spot them easily among table and column names.
  • Start SELECT, FROM, WHERE, and other main parts of your query on a new line.
  • The semicolon ; at the end of a query is the standard statement separator. It’s needed when you run several queries in a row in one script or code block. We recommend always ending every SQL query with a semicolon.

Example of good formatting:


SELECT customer_id, full_name, email, registration_date
FROM customers
WHERE delivery_address = 'Anytown';

You could write this query in other ways, using different formatting. Don’t stress too much about it right now. Just remember, there’s no single mandatory formatting style in SQL: the main thing is to write clear, readable code.

2
Task
SQL SELF, level 1, lesson 3
Locked
Commenting a Query
Commenting a Query
2
Task
SQL SELF, level 1, lesson 3
Locked
Let's uncomment the query
Let's uncomment the query
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION