19th Sep 2024 10 minutes read SQL Basics: The Complete Resource List Tihomir Babic sql learn sql Table of Contents SQL Cheat Sheet SQL Basics Courses on LearnSQL.com Your Guide to the SQL Basics SELECT WHERE Comparison Operators Arithmetic Operators Logical Operators AND, OR, and NOT Operators ORDER BY GROUP BY JOIN INSERT, UPDATE, DELETE Time to Practice SQL Basics! If you want to work with data, you have to start by learning the SQL basics! In this article, you’ll get an overview of core SQL concepts and syntax. Plus, we include links to great resources if you want to learn more! Structured Query Language, or SQL, is a programming language that first appeared in 1974. It is designed for managing data in relational databases, which means storing, changing, deleting, and querying the data inside the database. This is done using SQL’s basic statements: SELECT, INSERT, UPDATE, and DELETE. In this article, we will explain these statements and their elements. We’ll also provide you with a detailed list of further learning resources and talk about where you can get hands-on practice with SQL. SQL Cheat Sheet The most comprehensive quick resource for SQL’s core commands is our SQL Basics Cheat Sheet. This free resource gives you a thorough (but concise) overview of essential SQL concepts, including querying single and multiple tables, using operators, aggregating data, and performing set operations. It includes clear syntax examples and practical tips. You can bookmark the page or download the guide in PNG or PDF format, making it convenient to print or share with friends. SQL Basics Courses on LearnSQL.com If you want hands-on exposure to SQL, our SQL Basics courses are your best resource. They help you learn and review SQL basics, offering a structured approach with plenty of interactive exercises. We think it’s the most effective way to master SQL. Our SQL Basics course is designed to teach you the fundamentals of SQL, such as SELECT, WHERE, logical operators, comparison operators, JOINs, and more. We also cover a few more advanced topics, like GROUP BY, aggregate functions, and set operations (UNION, INTERSECT, and EXCEPT). To complete the course, you must solve 129 coding challenges. That’s quite a lot of practice! For those looking for even more SQL practice, our SQL Practice Set provides more coding challenges. There are 88 of them for refining your grasp of SQL basics. This course is a part of the broader SQL Practice track. Use this track and its ten courses to review and consolidate your SQL skills. Now let’s dig into the very heart of SQL basics: core concepts like SELECT, WHERE, and other key commands and clauses. Your Guide to the SQL Basics SELECT The SELECT statement in SQL fetches data from a database table. It can retrieve all the data in the table, or you can specify particular columns and rows. The basic SELECT syntax is: SELECT column_1, column_2, … FROM table; The columns you want to output are specified in the SELECT, and the table where they can be found is referenced in FROM. In the following sections, we will talk about using additional clauses and operators in SELECT. To better understand how they work, you must be familiar with the order of operations in SQL. Further reading: How To Learn The SELECT Statement in SQL SQL Order of Operations WHERE The WHERE clause filters rows in the SELECT output. When you use WHERE syntax to set up a filtering condition, the query checks the data and returns only rows that meet the condition. Here’s what WHERE syntax looks like: SELECT column_1, column_2, … FROM table WHERE condition; The WHERE clause comes after FROM and contains filtering conditions (which we’ll discuss in the following sections). Only data that meets these conditions will be shown in the query results. Further reading: How to Write a WHERE Clause in SQL 20 SQL WHERE Practice Exercises for Beginners HAVING vs. WHERE in SQL: What You Should Know Comparison Operators There are many basic SQL operators, including comparison, arithmetic, and logic operators. Comparison operators are used to compare text, numerical, and date/time data. Although these operators can compare different types of data, it’s important to note that you can only compare any type of data to itself; e.g. you cannot compare text data against numerical data. Here are some common comparison operators : Equals (=) returns values equal to (or strictly matching) the specified value. Greater than (>)returns values greater than the specified value. Less than (<)returns values lower than the specified value. Not equal (<>)returns values not equal to the specified value. Here’s the comparison operator syntax using the ‘equals’ operator. SELECT column_1, column_2, … FROM table WHERE column_1 = value_1; Arithmetic Operators SQL basics also include arithmetic operators, such as addition (+), subtraction (-), multiplication (*), and division (/). They are used to perform arithmetic operations on numerical data. Here’s the syntax of the division operator: SELECT column_1/column_2 AS column_alias, column_3, … FROM table WHERE column_3 = value_3; Logical Operators Logical operators allow you to further control, refine, and combine the conditions imposed by other operators. They also facilitate pattern matching. BETWEEN allows you to select a data range, i.e. data falling between the upper and lower limit: SELECT column_1, column_2, … FROM table WHERE column_1 BETWEEN value_1 AND value_2; The IN operator returns all data equal to any value in a list. When you specify a value list, IN basically functions as a shorthand for writing multiple OR conditions. Instead of a specific list, you can also use a subquery in the parentheses; IN will compare data to the subquery’s output. Here’s an example of IN syntax with a list of values: SELECT column_1, column_2, … FROM table WHERE column_1 IN (value_1, value_2, value_3, …); Further reading: How the Division Operator Works in SQL The SQL BETWEEN Operator SQL IN Operator How to Use Comparison Operators with NULLs in SQL LIKE is a logical operator used for filtering textual (string) data. It matches patterns – i.e. it returns strings that match a defined pattern. LIKE is commonly used with SQL wildcards, which substitute one or more characters in the string pattern. Here’s an example of LIKE syntax: SELECT column_1, column_2, … FROM table WHERE column_1 LIKE 'string_pattern'; Further reading: What Do the Operators LIKE and NOT LIKE Do? AND, OR, and NOT Operators These three operators are a family of logical operators in SQL. The AND and OR operators combine two or more filtering conditions. AND returns data that satisfies all filtering conditions, while OR returns data that satisfies one of two or more conditions. Here’s AND syntax: SELECT column_1, column_2, … FROM table WHERE column_1 = value_1 AND column_2 = value_2; The OR operator is used the same way; simply replace AND with OR. The NOT operator negates the filtering condition, so it returns data that doesn’t satisfy the condition. Here’s an example of NOT syntax: SELECT column_1, column_2, … FROM table WHERE NOT column_1 = value_1; Further reading: Using AND, OR, and NOT Operators in SQL ORDER BY Mastering SQL basics means knowing how to sort the data in the query result. This is done using the ORDER BY clause. It allows you to sort data in ascending (ASC) or descending (DESC) order: SELECT column_1, column_2, … FROM table ORDER BY column_1 [ASC|DESC]; Sorting is performed by one or more columns listed in the ORDER BY clause. You can practice sorting data with these seven ORDER BY examples. However, you can also sort data using the ORDER BY 1 syntax. Those just starting with SQL basics are often confused with the concepts of sorting and grouping data, so we wrote an article about the difference between ORDER BY and GROUP BY. Let’s point out that their differences don’t mean these two clauses can’t be used together. In fact, they are very often used in the same query. Read our guide on using GROUP BY and ORDER BY in the same query to learn more. Further reading: SQL ORDER BY Clause with 7 Examples What Does ORDER BY 1 Mean in SQL? Using GROUP BY and ORDER BY Together: A Guide Difference between GROUP BY and ORDER BY in Simple Words A Detailed Guide to SQL ORDER BY GROUP BY GROUP BY puts data with the same values into the one group. Grouping is performed by one or more columns specified in GROUP BY, as shown below: SELECT column_1, SUM(column_2) AS column_alias, … FROM table GROUP BY column_1; All rows that have the same column_1 value will be placed in one group. On its own, GROUP BY doesn’t have significant practical use. However, GROUP BY and aggregate functions make a power couple that makes it possible to perform aggregate calculations – counting, summing, averaging, and finding minimum and maximum values – for each group. Further reading: GROUP BY and Aggregate Functions: A Complete Overview JOIN The JOIN clause allows you to use data from two or more tables in the same SELECT. It does so by joining tables on a common column. By connecting tables that way, you can query all the columns as if they belong to a single big table. Here is the basic JOIN syntax: SELECT column_1, column_2, … FROM table_1 JOIN table_2 ON table_1.column_1 = table_2.column_1; There are different types of JOINs, such as INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, and CROSS JOIN. While they all serve the same purpose – joining tables – each type provides slightly different results. Further reading: Your Complete Guide to SQL JOINs (with Resources) INSERT, UPDATE, DELETE The most basic SQL statements, apart from SELECT, include INSERT, UPDATE, and DELETE. However, SELECT simply returns data from a database; these three statements allow you to change the data in the database. INSERT adds new data to a database using this syntax: INSERT INTO table (column_1, column_2, …) VALUES (value_1, value_2, …); The table into which data will be added is specified after INSERT INTO. It is then followed by the columns to which you want to add data. After the keyword VALUES, you put a list of values corresponding with the column list – i.e. value_1 will be added to column_1, value_2 to column_2 and so on. The UPDATE statement is used for updating existing data in a database. Here’s its syntax: UPDATE table SET column_1 = value_1, column_2 = value_2, … WHERE condition; After referencing the table you want to update, you set values to columns; i.e. the current value(s) in column_1 will be updated to value_1, etc . UPDATE is typically used with WHERE to update only data in rows that satisfy a specific condition (e.g. you could update an employee’s salary by filtering for their employee ID number; only rows with that ID number will have the updated salary). If you don’t use WHERE, all the records in the table will be updated. Finally, DELETE is for deleting data from a database: DELETE FROM table WHERE condition; The keyword DELETE is followed by the name of the table you want to delete records FROM and then the WHERE clause. That way, you will delete only records that satisfy a filtering condition; omitting WHERE will delete all the records in the table. If you want to master the INSERT, UPDATE, or DELETE commands in SQL, our course How to INSERT, UPDATE, and DELETE Data in SQL is the perfect way to gain hands-on experience in adding and modifying database records. Further reading: SQL INSERT, SQL UPDATE, SQL DELETE – Oh My! What Is the INSERT Statement in SQL? What Is the UPDATE Statement in SQL? What Is the DELETE Statement in SQL? Time to Practice SQL Basics! In this overview of SQL basics, we briefly explained the syntax of core SQL statements: SELECT, INSERT, UPDATE, and DELETE. This syntax very often involves different keywords and operators, which is especially the case with SELECT. It includes the WHERE clause and different operators for filtering data, ORDER BY for sorting it, GROUP BY for grouping it, and JOIN for querying data from multiple tables. Our short overview of all these keywords and operators can be complemented by the numerous articles we’ve linked to. Check them out if you want more information! Remember, all these topics are comprehensively covered in our SQL Basics course. What you learn there you can continue practicing in the SQL Practice set or the whole SQL Practice track. Happy learning, and we hope you enjoy mastering the SQL basics! Tags: sql learn sql