List Headline Image
Updated by Santosh Sahu on Aug 25, 2020
 REPORT
Santosh Sahu Santosh Sahu
Owner
9 items   1 followers   9 votes   1 views

SQL Tutorial

SQL stands for Structured Query Language. It is a query language and used for performing operations on the database such as insert new records, update records, delete records and view records etc. SQL queries are performed on database like Oracle database, MySQL database, and SQL Server database etc. All database management system uses SQL as standard database language.

Source: https://www.alphacodingskills.com/sql/sql-tutorial.php

The SQL WHERE Clause is used to specify condition(s) in SQL query. It is used to specify condition(s) while fetching data from a table, joining two tables, updating records in a table, inserting records in a table or deleting records from a table.

The SQL SELECT statement is used to select data from a database table and the selected data is returned in the form of a table.

Syntax
The syntax for using SELECT statement in different scenarios are given below:

/select one column/
SELECT column1 FROM table_name;

/select multiple columns/
SELECT column1, column2, ...
FROM table_name;

/select all columns of a table/
SELECT * FROM table_name;

The SQL SELECT DISTINCT statement is used to select different (distinct) data from a database table. It eliminates any duplicate records and fetches only unique records.

The syntax for using DISTINCT keyword is given below:

SELECT DISTINCT column1, column2, ....
FROM table_name;

Operators are used to perform operation on two operands. Operators in SQL can be categorized as follows:

  • Arithmetic operators
  • Comparison operators
  • Logical operators

The SQL LIKE clause is used in a SQL WHERE clause to search for a specified pattern in a specified column. There are two wildcards which are often used in conjunction with the LIKE operator.

  • % : represents zero, one or multiple characters.
  • _ : represents one character.

Note: In MS Access, asterisk (*) is used instead of percentage sign (%) and a question mark (?) is used instead of (_).

The SQL WHERE clause can be combined using AND, OR, and NOT operators. These operators are used to hadle more than one conditions.

  • AND: used to combine multiple conditions and displays a record if all the conditions separated by AND operator are true.
  • OR: used to combine multiple conditions and displays a record if any of the conditions separated by OR operator is true.
  • NOT: used to display a record where the specified condition is not true.

The SQL TOP Clause is used to fetch specified number or percentage of records from a table. This is useful when the table contains thousands of records and returning a large dataset can impact performance.

Note: TOP clause is not supported in all database. For example MYSQL supports the LIMIT clause and Oracle uses ROWNUM keyword to fetch limited number of records.

The SQL ORDER BY keyword is used to sort the result table in ascending or descending order. By default, ORDER BY keyword sorts the result in ascending order, however it can be specified using ASC keyword. To sort the result in descending order, DESC keyword is used.

The syntax for using ORDER BY keyword is given below:

SELECT column1, column2, column3, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

The SQL GROUP BY keyword is used to arrange result table into identical groups with the help of aggregate functions (COUNT, MAX, MIN, SUM, AVG etc). The GROUP BY keyword follows the WHERE clause in a SELECT statement and precedes the ORDER BY keyword.

The syntax for using SELECT statement in different scenarios are given below:

SELECT column1, column2
FROM table_name
WHERE condition(s)
GROUP BY column1, column2
ORDER BY column1, column2;