SQL (Structured Query Language) is a programming language used for managing and manipulating relational databases. It provides a standardized way to communicate with a Database Management System (DBMS) to perform various operations on databases. Let’s explore the syntax and structure of SQL.
Table of Contents
ToggleSQL Statements
SQL is composed of statements that are used to interact with the database. The most common SQL statements include:
- SELECT: Retrieves data from one or more tables.
- INSERT: Adds new records into a table.
- UPDATE: Modifies existing records in a table.
- DELETE: Removes records from a table.
- CREATE: Creates a new database, table, or other database objects.
- ALTER: Modifies the structure of a table or other database objects.
- DROP: Deletes a database, table, or other database objects.
- GRANT: Grants permissions to users or roles.
- REVOKE: Revokes permissions from users or roles.
We use these commands to create and manipulate data within database or database tables.
SQL Clauses
SQL statements consist of various clauses that provide additional instructions and conditions. Some commonly used clauses include:
- SELECT: Specifies the columns to retrieve data from or manipulate.
- FROM: Specifies the table(s) from which to retrieve data.
- WHERE: Filters records based on specific conditions.
- GROUP BY: Groups records based on a specified column(s).
- HAVING: Filters groups based on specific conditions.
- ORDER BY: Sorts the result set based on specified column(s).
- JOIN: Combines data from multiple tables based on related columns.
- UNION: Combines the result sets of two or more SELECT statements.
- LIMIT/OFFSET: Limits the number of rows returned or skips a specified number of rows.
- AND/OR/NOT: Logical operators used to combine conditions.
What is a difference between a statement and a clause?
Any complete line of code for a particular operation is called a statement. While clause is used to filter or order data or records like: where, having, order by etc.
SQL Syntax:
SELECT column1, column2
FROM table1
WHERE condition;
SQL Data Types
SQL supports various data types that define the nature of the data stored in columns. Common data types include:
- INTEGER: Whole numbers.
- FLOAT/REAL: Floating-point numbers.
- VARCHAR/CHAR: Variable-length/ fixed-length character strings.
- DATE/DATETIME: Date and time values.
- BOOLEAN: True or false values.
- BLOB: Binary large objects for storing binary data.
- NUMERIC/DECIMAL: Fixed-precision numeric values.
SQL Comments
Comments are used to add explanatory notes or documentation within SQL code. In SQL, comments can be single-line or multi-line:
- Single-line comments: Begin with “–” or “//” and continue until the end of the line.
- Multi-line comments: Enclosed between “/” and “/” and can span multiple lines.
-- This is a single-line comment
/*
This is a multi-line comment.
It can span multiple lines.
*/
SELECT column1, column2
FROM table1
WHERE condition;
Understanding the syntax and structure of SQL is crucial for working with databases effectively. It allows you to construct queries, retrieve and manipulate data, and perform various operations on databases. As you progress, you’ll learn more advanced SQL techniques such as joins, subqueries, functions, and stored procedures to enhance your data manipulation capabilities.