Open In App

SQL Syntax

Last Updated : 24 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Structured Query Language (SQL) is a standardized Query language essential for managing and manipulating relational databases. Mastery of SQL syntax is crucial for writing effective SQL queries and ensuring proper interaction with database management systems (DBMS). This guide will provide a thorough understanding of SQL syntax, enhancing clarity and readability in your SQL statements.

Introduction to SQL Syntax

SQL syntax is a well-defined set of rules and guidelines that must be followed when writing SQL queries. These rules ensure that queries are interpreted correctly by the database management system (DBMS). SQL is case-insensitive, meaning keywords can be written in either uppercase or lowercase. However, it is a common convention to write SQL keywords in uppercase to enhance readability.

Key Points of SQL Syntax

  • Case Sensitivity: SQL keywords are not case-sensitive. SELECT is the same as select.
  • Statement Termination: SQL statements typically end with a semicolon (;). This is essential for separating multiple SQL statements in a single execution.

Database Tables

A relational database consists of one or more tables. Each table contains records (rows) and fields (columns). For instance, consider a table named "Employee":

EmployeeIDFirstNameLastNameBirthDateHireDateDepartmentPositionSalary
1JohnDoe1985-06-152010-08-01ITSoftware Engineer75000
2JaneSmith1990-02-252012-05-15HRHR Manager65000
3EmilyJohnson1982-11-302008-09-12FinanceAccountant60000
4MichaelBrown1978-01-202005-03-21MarketingMarketing Director90000
5SarahDavis1995-07-102018-07-22ITSystem Analyst70000

SQL Statements

SQL statements are commands that perform specific actions on the database. These actions can range from querying data to modifying the structure of the database. Below are some of the most fundamental SQL statements:

1. SELECT Statement

The SELECT statement retrieves data from a database. It is one of the most commonly used SQL commands.

SELECT * FROM Employee;

2. INSERT INTO Statement

The INSERT INTO statement adds new rows to a table.

INSERT INTO Employee (EmployeeID, FirstName, LastName, BirthDate, HireDate, Department, Position, Salary)
VALUES (6, 'David', 'Wilson', '1992-03-18', '2020-09-15', 'Sales', 'Sales Manager', 68000);

3. UPDATE Statement

The UPDATE statement modifies existing records in a table.

UPDATE Employee
SET Salary = 80000
WHERE EmployeeID = 1;

4. DELETE Statement

The DELETE statement removes existing records from a table.

DELETE FROM Employee
WHERE EmployeeID = 2;

5. ALTER TABLE Statement

The ALTER TABLE statement modifies an existing table.

ALTER TABLE Employee
ADD COLUMN Email VARCHAR(255);

6. DROP TABLE Statement

The DROP TABLE statement deletes an existing table from the database.

DROP TABLE Employee;

7. WHERE Clause

The WHERE clause filters records based on specified conditions.

SELECT * FROM Employee
WHERE Department = 'IT';

8. ORDER BY Clause

The ORDER BY clause sorts the result set.

SELECT * FROM Employee
ORDER BY Salary DESC;

9. GROUP BY Clause

The GROUP BY clause groups rows that have the same values in specified columns.

SELECT Department, COUNT(*) AS EmployeeCount
FROM Employee
GROUP BY Department;

10. HAVING Clause

The HAVING clause filters groups based on conditions.

SELECT Department, AVG(Salary) AS AvgSalary
FROM Employee
GROUP BY Department
HAVING AVG(Salary) > 70000;

11. COUNT Function

The COUNT function returns the number of rows that match the criteria.

SELECT COUNT(*) AS TotalEmployees
FROM Employee;

12. SUM Function

The SUM function calculates the total sum of a numeric column.

SELECT SUM(Salary) AS TotalSalary
FROM Employee;

13. AVG Function

The AVG function calculates the average value of a numeric column.

SELECT AVG(Salary) AS AverageSalary
FROM Employee;

14. MIN and MAX Functions

The MIN and MAX functions return the smallest and largest values in a column, respectively.

SELECT MIN(Salary) AS MinimumSalary, MAX(Salary) AS MaximumSalary
FROM Employee;

Some Important SQL Commands

Conclusion

SQL syntax is essential for interacting with databases, enabling users to perform a variety of operations, from querying data to modifying database structures. Understanding and mastering these SQL commands and their syntax is crucial for efficient database management. This guide provides a foundational understanding, and further practice and exploration will solidify these concepts.


Next Article
Article Tags :

Similar Reads