A Quick Guide to SQL — Chapter 1: Introduction and Basic Syntax
SQL (Structured Query Language) is a powerful language used for managing and manipulating relational databases. In this lesson, we will cover the fundamental aspects of SQL syntax, including the basic structure of SQL statements, creating tables, and retrieving data using the SELECT statement.
Chapters of This Series
- Chapter 1: Introduction and Basic Syntax
- Chapter 2: Filtering and Sorting Data
- Chapter 3: Manipulating Data
- Chapter 4: Querying Multiple Tables with JOIN
- Chapter 5: Aggregating Data
- Chapter 6: Modifying Data
- Chapter 7: Advanced SQL Concepts
- Chapter 8: Modifying Table Structure
1. Understanding the Basic Structure of SQL Statements
SQL statements are composed of keywords, table names, column names, and optional clauses. The basic structure of an SQL statement is as follows:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
- SELECT: Specifies the columns to be retrieved from the table.
- FROM: Specifies the table from which the data will be retrieved.
- WHERE: Optional clause used to filter the data based on specified conditions.
2. Creating a Simple Table
Before we can retrieve data, we need a table. Let’s create a simple table called “employees” with a few columns.
CREATE TABLE employees (
id INT,
name VARCHAR(50),
age INT,
salary DECIMAL(10, 2)
);
In this example, we create a table named “employees” with four columns: “id” of type INT, “name” of type VARCHAR(50), “age” of type INT, and “salary” of type DECIMAL(10, 2).
3. Retrieving Data Using SELECT Statement
To retrieve data from the “employees” table, we use the SELECT statement. Let’s retrieve all the columns from the table:
SELECT * FROM employees;
The asterisk (*) in the SELECT statement indicates that we want to retrieve all columns from the table.
4. Filtering Data Using WHERE Clause
To retrieve specific rows based on certain conditions, we can use the WHERE clause. For example, let’s retrieve employees whose age is greater than 30:
SELECT * FROM employees WHERE age > 30;
In this query, we specify the condition “age > 30” in the WHERE clause to filter the data.
5. Combining Multiple Conditions Using AND and OR Operators
To filter data based on multiple conditions, we can use the AND and OR operators. For instance, let’s retrieve employees whose age is greater than 30 and salary is above $50,000:
SELECT * FROM employees WHERE age > 30 AND salary > 50000;
Here, we use the AND operator to combine the two conditions.
6. Sorting Retrieved Data Using ORDER BY Clause
To sort the retrieved data in a specific order, we can use the ORDER BY clause. Let’s retrieve employees’ data sorted by their salaries in descending order:
SELECT * FROM employees ORDER BY salary DESC;
The DESC keyword is used to specify descending order. For ascending order, we can use the ASC keyword, which is the default.
7. Conclusion
In this lesson, we covered the basic syntax of SQL statements. We learned about the structure of SQL statements, created a table, and retrieved data using the SELECT statement. We also explored how to filter data using the WHERE clause, combine multiple conditions using AND and OR operators, and sort retrieved data using the ORDER BY clause. This foundation will serve as the basis for more advanced SQL concepts in subsequent lessons.
If you found this course helpful and would like to explore more free courses, I invite you to follow my account on Medium and connect with me on LinkedIn. I regularly share valuable content on these platforms.