16th May 2023 11 minutes read The Most Important SQL Commands Dmitri Vaitkun SQL basics Table of Contents SQL Commands You Need to Know SELECT WHERE INSERT UPDATE DELETE ORDER BY GROUP BY JOIN CREATE ALTER Next Up: Practicing these Popular SQL Commands In this article, we'll explore the most common SQL commands that everyone should know, including SELECT, INSERT, WHERE and more. We’ll also give examples of how to use each command. Are you ready to dive into the world of databases? SQL, or Structured Query Language, is a programming language that is designed specifically to interact with databases. It allows you to manipulate the data in databases in multiple ways – e.g. alter it, delete, add it, or simply extract it and put it into a report. SQL is used by database designers, developers, and administrators, but it’s also used by data analysts and scientists to extract and analyze data from large relational databases. SQL is over 40 years old, but it is still massively used today. Why is it so popular? Databases are essential for many businesses, and SQL is a perfect tool to communicate with them. It is very efficient, optimized and flexible. And SQL has a simple syntax, which makes it easy to learn and use. You don’t have to be a programmer to understand what’s going on in a query. These features have made SQL a popular language for managing and analyzing data, and it is likely to remain so for many years to come. Sure, SQL may still take some time to learn, but if you follow our beginner tips for learning SQL, the process will be interesting and enjoyable. SQL Commands You Need to Know Learning the most common SQL commands is a great way to kick start your journey into the world of databases and SQL. And the best way to completely understand newly learned knowledge is to put it into practice! That’s why we are happy to offer you the terrific SQL from A to Z track, which you can start right after reading this article! This fully interactive series of courses contain all the essential information that every SQL specialist needs to know. You will not only be learning important commands; you’ll also be trying them out in realistic scenarios. After completing the track, you'll know how to create basic and advanced queries, connect tables, make SQL reports, and even use window functions. And now, let’s start with the most important SQL commands. SELECT The SELECT statement is often described as the most important SQL command. Most of the queries you as a SQL student or specialist will write will start with this command. SELECT is used to retrieve data from a database. The syntax for the SELECT statement is as follows: SELECT column1, column2, ... FROM table_name; Here, column1, column2, ... are the names of the columns you want to retrieve data from. The table_name is the name of the table from which you want to retrieve data. You can also use the * symbol to retrieve data from all columns in a table. For example, if you want to retrieve all the data from a table named customers, you would use the following SQL statement: SELECT * FROM customers; To see more examples of SELECT, check out our article How Do You Write a SELECT Statement in SQL? WHERE This SQL command allows you to filter the data you select. The WHERE clause follows the FROM clause in a SELECT, UPDATE, or DELETE statement. It specifies one or more conditions that must be met for the statement to execute. It is often accompanied by one or more logical operators or comparison operators. The basic syntax of a WHERE clause is: SELECT column1, column2, ... FROM table_name WHERE condition; In this syntax, column1, column2, … are the names of the columns that store the data you want; table_name is the name of the table that contains the data. The condition parameter specifies the condition that must be met for the statement to execute. The condition parameter can include one or more logical operators, such as AND, OR, and NOT; it can also have one or more comparison operators, such as is equal to (=), does not equal (<>), less than (<), greater than (>), less than or equal to (<=), and greater than or equal to (>=). You can also use functions and subqueries to create more complex conditions. Let’s look at a simple example. Let's say you have a table named employees with the columns id, name, age, and department. You want to retrieve the information for all employees who are younger than 30 years old. To do this, you can use the following SQL statement: SELECT * FROM employees WHERE age < 30; In this example, the WHERE clause is used to specify the condition that only employees with an age less than 30 will be included in the result set. This means that the result of this query will be all the rows in the employees table where the value in the age column is less than 30. Mastering WHERE is essential for everyone who wants to use SQL. Our complete guide to the WHERE command has more information and examples. INSERT The INSERT statement is one of SQL commands that allow you to change data in database tables; it adds new data to a table. The syntax for the INSERT statement is as follows: INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...); Here, table_name is the name of the table, column1, column2, ... are the names of the columns you want to add data to, and value1, value2, ... are the values you want to add to those columns. For example, if you want to add a new customer to the table customers, you would use the following SQL statement: INSERT INTO customers (customer_name, customer_email, customer_phone) VALUES ('John Doe', 'john.doe@example.com', '123-456-7890'); You can find out more about this SQL command in the article What Is the INSERT Statement in SQL?. UPDATE The UPDATE statement is used to modify existing data in a database. The syntax for the UPDATE statement is as follows: UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; Here, table_name is the name of the table. Next, column1, column2, ... are the names of the columns you want to modify, and value1, value2, ... are the new values you want to set for those columns. Finally, condition is the criteria for selecting which rows to modify. For example, if you want to update the email address for a customer with the name "John Doe" in the table customers, you would use the following SQL statement: UPDATE customers SET customer_email = 'johndoe@example.com' WHERE customer_name = 'John Doe'; DELETE The DELETE statement is used to remove data from a database. The syntax for the DELETE statement is as follows: DELETE FROM table_name WHERE condition; Here, table_name is the name of the table you want to remove data from, and condition is the criteria for selecting which rows to remove. For example, if you want to delete the customer with the name "John Doe" from the table customers, you would use the following SQL statement: DELETE FROM customers WHERE customer_name = 'John Doe'; There are two more SQL commands that do what at first seems a similar job – TRUNCATE and DROP. You’ll often be asked about the differences between these commands in SQL job interviews. Luckily, we have an article for this: TRUNCATE TABLE vs. DELETE vs. DROP TABLE: Removing Tables and Data in SQL. ORDER BY The ORDER BY clause is used to sort the result set of a SELECT statement in either ascending (A-Z, 1-10) or descending (Z-A, 10-1) order. The syntax for the ORDER BY clause is as follows: SELECT column1, column2, ... FROM table_name O ORDER BY column_name ASC|DESC; Here, column_name is the name of the column you want to sort the result set by and ASC or DESC specifies whether the sort order should be ascending or descending. If the order is not specified (neither ASC or DESC are written), the order is set to ascending by default. For example, if you want to retrieve all the customers from a table named customers and sort them by their names in ascending order, you would use the following SQL statement: SELECT * FROM customers ORDER BY customer_name ASC; There are more tricks this SQL command can do, like sorting by multiple columns. If you want to learn more, our Detailed Guide to ORDER BY was written just for you. GROUP BY The GROUP BY clause is used to group rows that have the same values in a given column. You’ll often use GROUP BY when the task is something like "find the average price per product category". The syntax for the GROUP BY clause is as follows: SELECT column1, column2, ... FROM table_name GROUP BY column_name; Here, column_name is the name of the column you want to group by. To get the average price per product category from the table products, you’d use the following SQL statement: SELECT category, AVG(price) FROM products GROUP BY category; For a detailed explanation and more examples, read our article What Is GROUP BY in SQL?. JOIN The JOIN clause is used to combine rows from two or more tables based on matching values in a stated column. There are different types of JOIN clauses in SQL, including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. The syntax for the INNER JOIN clause is as follows: SELECT column1, column2, ... FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name; Here, table1 and table2 are the names of the tables you want to join, and column_name is the name of the column that is used to relate the two tables. For example, if you want to retrieve all the orders with their corresponding customer names from two tables named orders and customers, you would use the following SQL statement: SELECT orders.order_id, customers.customer_name FROM orders INNER JOIN customers ON orders.customer_id = customers.customer_id; This query looks at the customer_id column in orders and at the customer_id column in customers. If these two columns have matching values, the query will return the order ID from the orders table along with the customer_name from the customers table. The matching customer IDs mean that this customer placed this order. JOIN is one of the more complicated and diverse of common SQL commands. If you want to learn more, check out our article What Are the Different SQL JOIN Types?. CREATE The CREATE statement is used to create a new database object, such as a table, view, or index. The syntax for the CREATE statement varies depending on the type of object you want to create. Here is an example of how to create a new table: CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, ... ); Here, table_name is the name of the table you want to create, and column1, column2, column3, ... are the names of those columns. The type of data each column stores (text, integers, decimal numbers, etc.) is indicated by the datatype. For example, if you want to create the new table products with columns for the product ID, product name, and price, you would use the following SQL statement: CREATE TABLE products ( product_id INT PRIMARY KEY, product_name VARCHAR(50), price DECIMAL(10, 2) ); You can learn more about the PRIMARY KEY and other aspects of creating a table in our article How to Create Your First Table in SQL. ALTER The ALTER statement is used to modify the structure of an existing database object, such as a table or view. The syntax for the ALTER statement varies depending on the type of object you want to modify. Here is an example of how to add a new column to an existing table: ALTER TABLE table_name ADD column_name datatype; Here, table_name is the name of the table you want to modify, column_name is the name of the new column, and datatype is the data type of the new column. For example, if you want to add a new column named description to an existing table named products, you would use the following SQL statement: ALTER TABLE products ADD description VARCHAR(100); Another example of using the ALTER statement is to modify an existing column's datatype or size. For example, if you want to change the datatype of the price column in the products table from DECIMAL(10,2) to DECIMAL(12,2), you would use the following SQL statement: ALTER TABLE products ALTER COLUMN price DECIMAL(12,2); Next Up: Practicing these Popular SQL Commands SQL is a powerful language that allows you to manage, manipulate, and retrieve data from databases. Understanding the most important SQL commands is essential to becoming a successful database developer or data analyst. By mastering SELECT, INSERT, UPDATE, DELETE, WHERE, ORDER BY, GROUP BY, JOIN, CREATE, and ALTER, you can efficiently perform a wide range of database operations – which opens for you a possibility to get a high-paying database job. Moreover, mastering these SQL commands is a fundamental step to explore more advanced SQL topics – such as subqueries, window functions, and advanced joins – that are necessary to handle complex data operations. And with that, you now know most of the important SQL commands. It’s the perfect moment to start practicing your new knowledge. For that, we recommend the SQL Basics course. There you will learn and polish your knowledge by practicing. You’ll use the code editor to run commands on a real database and real datasets. And you’ll solve realistic problems by coming up with your own SQL queries. If you're serious about a SQL career, you might want to check out our All Forever Package, a collection of all current and future SQL courses. There is no better offer on the market. Boost your chances of success! Tags: SQL basics