SQL Stands for Structure Query Language. It is an ANSI (American National Standard Institute) Standard.
SQL is a database programming language used for manipulating and managing relational database
Users can create complex queries and perform complex tasks efficiently using SQL and it is an essential tool for data management, analysis and reporting in various industries and applications.
What can SQL do ?
- Retrieve data from a database
- Insert records in a database
- Update records in a database
- Delete records from a database
- Create new database
- Create new tables in a database
- Set permissions
Basic Structure of SQL Statements
SQL statements have a specific structure that consists of different clauses.
-
SELECT Clause
-
FROM Clause
-
WHERE Clause
-
GROUP BY Clause
-
HAVING Clause
-
ORDER BY Clause
SELECT Clause
The SELECT clause is used to specify the columns or expressions to be retrieved from the database.
Synatx:
SELECT column1, column2 FROM table_name;
Example:
SELECTemp_id,emp_name, Phone FROM employee
FROM Clause
The FROM clause specifies the table or tables from which the data will be retrieved.
Synatx:
SELECT column1, column2 FROM table_name;
Example:
SELECTemp_id,emp_name, Phone FROM employee
WHERE Clause
The WHERE clause is used to filter the data based on specific conditions
Synatx:
SELECT column1, column2 FROM table_name WHERE condition;
Example:
SELECT emp_name, emp_id, phone, email FROM employee WHERE age > 20;
GROUP BY Clause
The GROUP BY is used to groups the rows based on the values in one or more columns and perform aggregate functions on each group.
SELECT column1, COUNT(*) FROM table_name GROUP BY column1 , Column 2..
Example:
SELECT COUNT(CustomerID)as Customer_count , Country FROM Customers GROUP BY Country ;
SELECT COUNT(CustomerID) as Customer_count, Country , Gender FROM Customers GROUP BY Country , Gender;
HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.
used to select records which satisfy the given condition and also it is used with aggregate function.
Synatx:
SELECT column1, COUNT(*) FROM table_name GROUP BY column1 HAVING condition;
Example:
SELECT COUNT(CustomerID) as Customer_count, Country
FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5;
ORDER BY Clause
The ORDER BY clause is used to sort the result set in ascending or descending order based on one or more columns.
Some databases sort the query results in an ascending order by default.
Syantx:
SELECT column1, column2 FROM table_name ORDER BY column1 [ASC | DESC];
Example:
SELECT NAME, AGE,SALARY FROM CUSTOMERS ORDER BY SALARY DESC;
SQL Basic Structure Example:
Synatx:
SELECT column1, column2
FROM table_name WHERE condition
GROUP BY column1 HAVING condition
ORDER BY column1 ASC
Example:
SELECT department_id, COUNT(*)as employee_count
FROM Employee WHERE age > 30
GROUP BY department_id HAVING COUNT(*) > 5
ORDER BY department_id ASC;
This query retrieves the department_id and the count of employees (employee_count) for each department from the Employee table. It only includes departments where the count of employees is greater than 5 and the employees’ age is over 30. The result is ordered by department_id in ascending order.