1. SELECT DISTINCT Statement
Keyword DISTINCT is used to return only different values.
Grammar:
SELECT DISTINCT column name from table name
2.SELECT into statement
The SELECT INTO statement selects data from one table and then inserts the data into another table.
The SELECT into statement is commonly used to create a backup copy of a table or to archive records.
SQL SELECT into Syntax
Insert all columns into the new table:
SELECT *
into new_table_name [in Externaldatabase]
From Old_tablename
Or just insert the desired column into the new table:
SELECT column_name (s)
into new_table_name [in Externaldatabase]
From Old_tablename
Where the IN clause can be used to copy tables to another database:
Example: SELECT * Into Persons in ' Backup.mdb ' from Persons
If we want to copy some of the fields, we can list them after the SELECT statement:
Example:SELECT lastname,firstname into Persons_backup from Persons
3.SQL Union operator
The Union operator is used to combine the result set of two or more SELECT statements.
Note that the SELECT statement inside the Union must have the same number of columns. The column must also have a similar data type.
Also, the order of the columns in each SELECT statement must be the same.
SQL Union Syntax
SELECT column_name (s) from table_name1
UNION
SELECT column_name (s) from table_name2
By default, the union operator chooses a different value. If duplicate values are allowed, use UNION ALL.
SQL UNION All Syntax
SELECT column_name (s) from table_name1
UNION All
SELECT column_name (s) from table_name2
In addition, the column name in the union result set is always equal to the column name in the first SELECT statement in the Union.
4.SQL JOIN
SQL join is used to query data from these tables based on the relationship between the columns in two or more tables.
Join and Key
Sometimes in order to get the complete result, we need to get the results from two or more tables. We need to execute the join.
Tables in the database can be linked by keys. The primary key (Primary key) is a column, and the value of each row in the column is unique.
in the table, each The value of a primary key is unique. The purpose of this is to cross-bind the data between tables without repeating all the data in each table.
different SQL JOIN
The following lists the types of joins you can use, and the differences between them.
- JOIN: Returns a row if there is at least one match in the table
- Left JOIN: Returns all rows from the table, even if there is no match in the right table
- Right JOIN: Returns all rows from the correct table even if there is no match in the left table
- Full JOIN: Returns a row if there is a match in one of the tables
5.SQL INNER Join keyword
the INNER join keyword Returns a row when there is at least one match in the table
INNER Join keyword Syntax
SELECT column_name (s)
From table_name1
INNER JOIN table_name2
On table_name1.column_name = Table_name2.column_name
6.SQL LEFT JOIN keyword
The LEFT JOIN keyword returns all of the rows from the table to the right, even if there are no matching rows in the Table_name2 table.
Left join keyword syntax
SELECT column_name (s)
From table_name1
Left JOIN table_name2
On table_name1.column_name = Table_name2.column_name
Note: In some databases, the left join is called the left OUTER join
(not finished, to be continued ....) )
Partial syntax collation in SQL