SQL is a standard language for accessing databases.
What is SQL?
- SQL stands for Structured Query Language
- SQL is ues to access and manipulate databases
- SQL is an ANSI (American National Standards Institute) standard
Some of The Most Important SQL Commands
- SELECT - extracts data from a database
- UPDATE - updates data in a database
- DELETE - deletes data from a database
- INSERT INTO - inserts new data into a database
- CREATE DATABASE - creates a new database
- ALTER DATABASE - modifies a database
- CREATE TABLE - creates a new table
- ALTER TABLE - modifies a table
- DROP TABLE - deletes a table
- CREATE INDEX - creates an index (search key)
- DROP INDEX - deletes an index
The SQL SELECT Statement
The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.
SQL SELECT Syntax
SELECT column_name,column_name FROM table_name;
and
SELECT * FROM table_name;
SELECT DISTINCT column_name,column_name FROM table_name;
WHERE
The WHERE clause is used to filter records.
WHERE column_name operator value;
The OR operator displays a record if either the first condition OR the second condition is true.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in a descending order, you can use the DESC keyword.
ORDER BY column_name ASC|DESC, column_name ASC|DESC;
The first form does not specify the column names where the data will be inserted, only their values:
INSERT INTO table_name VALUES (value1,value2,value3,...);
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
WHERE some_column=some_value;
The SELECT TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on performance.
Note: Not all database systems support the SELECT TOP clause.
FROM table_name;
SELECT column_name,column_name FROM table_name;
and
SELECT * FROM table_name;
DISTINCT
The DISTINCT keyword can be used to return only distinct (different) values.SELECT DISTINCT column_name,column_name FROM table_name;
WHERE
The WHERE clause is used to filter records.
SQL WHERE Syntax
SELECT column_name,column_name FROM table_nameWHERE column_name operator value;
AND & OR
The AND operator displays a record if both the first condition AND the second condition are true.The OR operator displays a record if either the first condition OR the second condition is true.
The SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set by one or more columns.The ORDER BY keyword sorts the records in ascending order by default. To sort the records in a descending order, you can use the DESC keyword.
SQL ORDER BY Syntax
SELECT column_name, column_name FROM table_nameORDER BY column_name ASC|DESC, column_name ASC|DESC;
The SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.SQL INSERT INTO Syntax
It is possible to write the INSERT INTO statement in two forms.The first form does not specify the column names where the data will be inserted, only their values:
INSERT INTO table_name VALUES (value1,value2,value3,...);
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
SQL UPDATE Statement
The UPDATE statement is used to update existing records in a table.SQL UPDATE Syntax
UPDATE table_nameSET column1=value1,column2=value2,...
WHERE some_column=some_value;
SQL DELETE Statement
The DELETE statement is used to delete rows in a table.SQL DELETE Syntax
DELETE FROM table_nameWHERE some_column=some_value;
SQL SELECT TOP Clause
The SELECT TOP clause is used to specify the number of records to return.The SELECT TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on performance.
Note: Not all database systems support the SELECT TOP clause.
SQL Server / MS Access Syntax
SELECT TOP number|percent column_name(s)FROM table_name;
No comments:
Post a Comment