20th Nov 2020 5 minutes read How To Learn The SELECT Statement in SQL Adrian Więch sql basics Table of Contents Hello World of SQL More Advanced SELECT Usages Where To Learn SQL SELECT? Summary When you think about learning SQL, one of the first things you come across is the SELECT statement. Selecting information is arguably the most important SQL feature. In this article, we’ll demonstrate the typical use cases for SQL SELECT with practical examples. Read this article if you’re a beginner database user and would like to know what SELECT can do for you. SQL, or Structured Query Language, is the programming language the IT world uses to communicate with databases. It first appeared in 1974, and has been in widespread use ever since. If you’re reading this article, you’re probably thinking about learning SQL, or you’ve just started your adventure with databases. In this article, we’ll explain how you can use the SQL SELECT statement, which is probably one of the most important SQL aspects. New to databases and SQL? Before continuing with the article, you can also take a look at two short YouTube videos that we’ve prepared. First, watch our video about databases. Once you’re through, have a look at another video that explains what SQL is. Hello World of SQL Whenever people go about studying a new programming language, they typically start with a metaphorical “Hello World” example that demonstrates the simplest instruction that can actually do something useful. What would that instruction be in SQL? Imagine we have a table in our database that contains some basic information about SQL enthusiasts. The table looks like this: Table name: sql_enthusiast first_namecountryyear_born JohnUK1974 PiotrPoland1985 RaquelArgentina1992 AikoJapan1960 In the world of SQL, the very first example for this table would be as follows: SELECT * FROM sql_enthusiast; Let’s break this example down: SELECT is an SQL keyword which indicates what we want to show (retrieve). * (asterisk) means “everything, all columns”. FROM is another SQL keyword which indicates the table(s) (i.e. the source of the data we need). sql_enthusiast is the name of the table we retrieve the data from. This instruction in the world of databases means “show all data from table sql_enthusiast”. You can see that this query already contains the SELECT statement, which is the command used to retrieve, or show, information from database tables. As a result, our database will produce something along the lines of: first_name | country | year_born ------------+-----------+---------- John | UK | 1974 Piotr | Poland | 1985 Raquel | Argentina | 1992 Aiko | Japan | 1960 In the example above, we used the asterisk sign (*) to show all the information from the table. However, we can also be more specific. Instead of the asterisk, we can provide a specific column name: SELECT first_name FROM sql_enthusiast; The instruction above is similar to the previous one. However, instead of SELECT *, which meant “show all columns”, we use SELECT first_name, which means: “select the column first_name”. The instruction above means “show all first names from the table sql_enthusiast”. As a result, we’ll see the following: first_name ----------- John Piotr Raquel Aiko Not so complicated, right? We can also specify more than one column to select (show) by separating the column names with commas: SELECT first_name, year_born FROM sql_enthusiast; This time, SELECT first_name, year_born means “select columns first_name and year_born”. The entire SQL query will show all first names alongside the corresponding birth years from the table sql_enthusiast. When we run this instruction in our database, we’ll get something like this in return: first_name | year_born -----------+---------- John | 1974 Piotr | 1985 Raquel | 1992 Aiko | 1960 Question: What is SQL SELECT? A SQL SELECT command retrieves data from a database. You can specify exactly what data you need from one or more tables by filtering for specific rows, choosing columns, and even performing calculations on the data. More Advanced SELECT Usages Judging from the examples above, the SQL SELECT statement may look pretty simple and straightforward to use. However, it can also come in handy in more complex queries. The SELECT statement can compute statistics for tables or groups of rows. How many SQL enthusiasts are there in our database? What is their average age? Who’s the oldest SQL enthusiast, and who’s the youngest? All of these questions can be answered with proper SELECT instructions. Apart from statistics for entire tables or groups of rows, we can use various functions and perform calculations in the SELECT clause too. What’s the date today? How many days have passed since a given date? For a table with product prices, what are the product prices before and after tax? What’s the difference between two columns? SQL SELECT can handle all of these situations too. In more advanced cases, the SELECT statement can even be used to filter rows based on complex conditions, join information from multiple tables, or present running averages. Where To Learn SQL SELECT? If you’d like to become fluent in SQL and learn how to use the SELECT statement and other SQL elements, we’ve got you covered. LearnSQL.com is a platform to learn SQL all by yourself from scratch. We teach you to use the database language in a completely interactive manner. If you’re a beginner or you’ve never written an SQL instruction before, have a look at our best-selling SQL Basics course. It will take you from zero to an intermediate SQL user in around 10 hours. If time is at stake, you can also read our article entitled How Long Does It Take to Learn SQL. If you’d like to find out more about the benefits of learning with us before you enroll, take a look at this article: Why Take the SQL Basics Course at LearnSQL.com. It explains in detail why we have built the course, what it contains and what you can expect inside. And if you have difficulties remembering the SQL keywords and syntax, we’ve prepared an SQL cheat sheet which you can download for free! It is available here: SQL Basics Cheat Sheet. Summary The SELECT statement is by far the most widely used SQL command when retrieving information from databases. Even though its main purpose is to show information from tables, it can also do much more than that. We hope this article gave you a good overview of the potential hidden in this SQL instruction. Tags: sql basics