3rd Dec 2020 5 minutes read How Do You Write a SELECT Statement in SQL? Kateryna Koidan sql basics Table of Contents What Is SQL? The SQL SELECT Statement: Basics The SQL SELECT Statement: Examples What’s Beyond the SELECT Statement? Let’s Practice the SELECT Statement! SQL is a language to communicate with relational databases, and the SELECT statement is the first thing you’ll learn when you start using SQL. In this article, you’ll learn how to write SELECT statements, from the basics to more advanced. What Is SQL? SQL stands for “Structured Query Language.” It has a long history that started in the 1970s. Being the standard for communication with relational databases, it has maintained its popularity. This domain-specific programming language is an effective and powerful tool for data management and access. Although it has limited usage compared to general-purpose programming languages, it remains a prerequisite for data-related jobs. Do you need data from Oracle or Microsoft SQL Server? SQL queries will get it for you. Start learning SQL today with this SQL Basics course. The SQL SELECT Statement: Basics SELECT is the basic statement in SQL used to get data out of a database. Databases keep their data in tables, which consist of columns and rows. In the SELECT statement, you start by choosing the columns you want from a certain database table. You can also filter rows in your SQL query, but we’re going to focus on the basic SELECT statement. The syntax of the SQL SELECT statement is pretty straightforward. Let’s say you have a table in your database with columns col_1, col_2, col_3, etc. If you need only the first column from this table, your SQL SELECT statement will look as follows: SELECT col_1 FROM table; That’s it! Pretty simple, right? If you want several columns from your table, you simply list the names of these columns in your SELECT statement: SELECT col_1, col_2, col_3 FROM table; Finally, if you want all of the columns from your database table, you can simply use the asterisk (*) instead of listing all of the column names: SELECT * FROM table; Now, let’s practice SELECT statements with some real examples. The SQL SELECT Statement: Examples For a fun first experience with SELECT statements, I suggest practicing with the following table, netflix, which lists some of the best-performing Netflix TV series. idtitlegenreyearseasonsepisodes 1The Queen's Gambitdrama202017 2The Haunting of Hill Househorror2018110 3Challenger: The Final Flightdocumentary202014 4Chewing Gumcomedy2015212 5The Umbrella Academycomedy2019220 6Orphan Blackscience fiction2013550 7Dear White Peoplecomedy2017330 To start, we only want the titles of the TV series. Our SELECT statement will look as follows: SELECT title FROM netflix; And here is the result: title The Queen's Gambit The Haunting of Hill House Challenger: The Final Flight Chewing Gum The Umbrella Academy Orphan Black Dear White People That’s great! Now, we want to know more about these TV series. Let’s add information about their genre and year of release: SELECT title, genre, year FROM netflix; The output of this SQL query will be the following: titlegenreyear The Queen's Gambitdrama2020 The Haunting of Hill Househorror2018 Challenger: The Final Flightdocumentary2020 Chewing Gumcomedy2015 The Umbrella Academycomedy2019 Orphan Blackscience fiction2013 Dear White Peoplecomedy2017 As you can see, the syntax is straightforward. You simply write which columns you want to SELECT and FROM which table. Finally, let’s say you want to see all of the available information from the netflix table. Just use the following query: SELECT * FROM netflix; Remember that the asterisk (*) says that we want to get all of the columns from our database table. The result will show the entire table: idtitlegenreyearseasonsepisodes 1The Queen's Gambitdrama202017 2The Haunting of Hill Househorror2018110 3Challenger: The Final Flightdocumentary202014 4Chewing Gumcomedy2015212 5The Umbrella Academycomedy2019220 6Orphan Blackscience fiction2013550 7Dear White Peoplecomedy2017330 What’s Beyond the SELECT Statement? Now you have some basic understanding of how SQL queries work. But, of course, there is much more you can do with SQL, and SELECT statements specifically. Some of the simplest actions include filtering data with the WHERE keyword and sorting the output with the ORDER BY keyword: SELECT * FROM netflix WHERE genre = 'comedy' ORDER BY year; With this simple query, we have selected the TV series in the comedy genre and sorted the output by the release year: idtitlegenreyearseasonsepisodes 4Chewing Gumcomedy2015212 7Dear White Peoplecomedy2017330 5The Umbrella Academycomedy2019220 However, don’t be tricked by the simplicity of these queries. SQL is a powerful tool for data access and management. In the SELECT statement, you can combine data from multiple tables, compute summary values, include complex filters, etc. Learn more in this comprehensive guide to the SELECT statement in SQL. Let’s Practice the SELECT Statement! If you want to learn SQL fast, you need lots of practice with real-world examples. I suggest starting with the SQL Basics course. This course covers simple queries performed on a single table and more advanced topics like joining multiple tables, filtering data with complex WHERE conditions, and writing subqueries. Plus, you’ll get the opportunity to practice these SQL topics with 129 interactive exercises. Interested in learning more? Join the SQL Fundamentals track. It includes two more courses in addition to SQL Basics and has over 390 interactive exercises in total. With this track, you’ll go beyond simple SELECT statements and learn how to INSERT, UPDATE, and DELETE data in SQL. You’ll also practice processing numerical, text, and other types of data. If you have any doubts about the effectiveness of online learning, check out this article that summarizes the best ways to practice SQL online. Thanks for reading, and happy learning! Tags: sql basics