SQL Syntax & Structure

The Structured Query Language (SQL) forms the backbone of modern relational databases. Mastering its syntax and structure empowers you to unleash the power of data, extracting insights and manipulating information with precision. This article delves into the basics of SQL, equipping you with the tools to craft effective queries and unlock the vast potential of your data.

Basic SQL Syntax

The basic syntax of SQL statements is as follows:

i. Statement

A SQL statement starts with a keyword that indicates the action to be performed. It is has the following syntax:

SQL
                        
KEYWORD clause1 clause2 ...;
                        
                    

ii. Clause

A clause is a part of a statement that specifies some condition or parameter

SQL
                        
clause ::= expression | operator | keyword
                        
                    

iii. Expression

An expression is a combination of values, variables, operators, and functions

SQL
                        
expression ::= value | variable | operator expression | function(expression)
                        
                    

iv. Operator

An operator is a symbol that performs some arithmetic or logical operation

SQL
                        
operator ::= + | - | * | / | = | < | > | AND | OR | NOT | IN | LIKE | etc.
                        
                    

v. Function

A function is a predefined or user-defined routine that returns a value

SQL
                        
function ::= SUM | AVG | MIN | MAX | COUNT | etc.
                        
                    

vi. Comment

A comment is a text that is ignored by the SQL engine. A comment starts with -- and ends with a newline. A comment can be placed anywhere in a SQL statement

SQL
                        
-- This is an SQL comment
                        
                    

Here is an example of a SQL statement that selects all records from a table named employees:

SQL
                        
-- Select all records from the employees table
SELECT * FROM employees;

Basic SQL Structure

The basic structure of SQL statements depends on the type and purpose of the statement. However, most SQL statements follow a common pattern that consists of three main clauses: SELECT, FROM, and WHERE.

  • The SELECT clause specifies what columns or expressions to return in the result set.
  • The FROM clause specifies what tables or sources to access to get the data.
  • The WHERE clause specifies what conditions or filters to apply to the data.

The basic structure of SQL statements with these clauses is as follows:

SQL
                        
-- A SQL statement with SELECT, FROM, and WHERE clauses
SELECT column1, column2, ... FROM table1, table2, ... WHERE condition1 AND condition2 ...;

Here is an example of a SQL statement that selects the first name, last name, and salary of the employees who work in the corporate department and earn more than 4000 USD:

SQL
                        
SELECT first_name, last_name, salary FROM employees WHERE department = 'Corporate' AND salary > 4000;
                        
                    

Conclusion

In this article, we have learned about the basic SQL syntax and structure. SQL is a language for querying and manipulating data in relational databases. SQL consists of keywords, clauses, expressions, and operators that form statements. SQL statements are used to perform various actions on a database, such as creating, updating, deleting, or retrieving data. The basic syntax of SQL statements follows a common pattern that consists of three main clauses: SELECT, FROM, and WHERE. These clauses specify what, from where, and how to get the data. SQL is a powerful and widely used language for data analysis and management.