SQL Commands: Main Tips
To interact with SQL databases, we need to know the SQL
commands. We also called SQL statements.
SQL is not case-sensitive, but writing SQL commands in ALL
CAPS improves readability of your code.
SQL Statement Syntax
Examples,
SELECT Statement
SELECT * FROM Users
The asterisk (*) sign defines all columns of the table. The
SELECT statement returns all columns of the Student Users. If you want Name,
Age only the try below query
SELECT Name, Age FROM Users
Filtering the Data: WHERE Clause
SELECT * FROM Users WHERE Age >=18
LIKE operator is a logical operator that provides to apply a
special filtering pattern to WHERE condition in SQL queries.
SELECT * FROM Users WHERE Name LIKE 'AN%'
IN operator enables us to apply multiple value filters to WHERE clause.
SELECT * FROM Users WHERE Name IN ('Anil','Aradhya')
The BETWEEN operator filters the data that falls into the
defined begin and end value.
SELECT * FROM Users WHERE Age BETWEEN 18 AND 22
Sorting the Data: ORDER BY Statement
The ORDER BY keyword is used to sort the result set in
ascending or descending order. The ORDER BY keyword sorts the records in
ascending order by default.
To sort the records in descending order, use the DESC keyword.
To sort the records in ascending order, use the ASE keyword.
SELECT * FROM Users ORDER BY Age DESC
SELECT * FROM Users ORDER BY Age ASC
Eliminating the Duplicate Data: DISTINCT Clause
The SELECT DISTINCT statement is used to return only
distinct values.
Inside a table Users, column Name can contains many
duplicate Name (like Anil, Anil) and sometimes you only want to list the
distinct values.
Stored Procedure | Functions | View – Virtual Table |
Trigger | Index - Clustered, Non-clustered | Cursor |
Joins | Error Handling - TRY CATCH | @@ERROR |
RAISERROR | @@ROWCOUNT | @@IDENTITY |