SQL QUERIES
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(s)
FROM table_name
FROM table_name
SELECT * FROM table_name
An SQL SELECT Example
The "Persons" table:P_Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Hansen | Oya | Timoteivn 10 | Sandnes |
2 | Svendson | mnve | Borgvn 23 | Sandnes |
3 | Pettersen | reri | Storgt 20 | Stavanger |
We use the following SELECT statement:
SELECT LastName,FirstName FROM Persons
The SQL SELECT DISTINCT Statement
In a table, some of the columns may contain duplicate values. This is not a problem, however, sometimes you will want to list only the different (distinct) values in a table.The DISTINCT keyword can be used to return only distinct (different) values.
SQL SELECT DISTINCT Syntax
SELECT DISTINCT column_name(s)
FROM table_name
FROM table_name
The WHERE Clause
The WHERE clause is used to extract only those records that fulfill a specified criterion.SQL WHERE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
FROM table_name
WHERE column_name operator value
The UPDATE Statement
SQL UPDATE Syntax
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
SET column1=value, column2=value2,...
WHERE some_column=some_value
The AND & OR Operators
The AND operator displays a record if both the first condition and the second condition is true.The OR operator displays a record if either the first condition or the second condition is true.
SELECT * FROM Persons
WHERE FirstName='ak'
OR FirstName='nk'
SELECT * FROM Persons WHERE
LastName='kumar'
AND (FirstName='ak' OR FirstName='nk')
LastName='kumar'
AND (FirstName='ak' OR FirstName='nk')
The ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set by a specified column.The ORDER BY keyword sort the records in ascending order by default.
If you want to sort the records in a descending order, you can use the DESC keyword.
SQL ORDER BY Syntax
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC
FROM table_name
ORDER BY column_name(s) ASC|DESC
SQL INSERT INTO Syntax
It is possible to write the INSERT INTO statement in two forms.The first form doesn't specify the column names where the data will be inserted, only their values:
INSERT INTO table_name
VALUES (value1, value2, value3,...)
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,...)
VALUES (value1, value2, value3,...)
SQL DELETE Syntax
DELETE FROM table_name
WHERE some_column=some_value
WHERE some_column=some_value
No comments:
Post a Comment