Rules to write SQL commands with some important SQL commands
Rules for SQL commands
1. SQL commands can be written on multiple line.
2. Clauses are generally used to separate lines to build accuracy through it is not necessary.
3. Tabulation (Index) can be used.
4. Command words cannot divide over the lines.
5. SQL commands are not case sensitive.
6. SQL commands is enrolled with SQL prompt. The SQL prompt, perform as a command line buffer.
7. Execution takes place when the statementis specified by a semi-colon(;).
8. Only one statement can be present at any time within the buffer and it can be run in multiple ways.
Place a semicolon (;) at the end of the last clause.
Place a semicolon (;) forward slash / on the last line in the buffer.
Place a forward slash at the SQL prompt.
Point a RUN command at the SQL prompt.
Some important SQL commands
These commands are very useful for the user to understand the structure of the table.
illustrate <table name>;
Example :-
illustrate <students>;
SELECT statements
1. SELECT command provides the query proficiency. It retrieves information and useful data from the table.
2. By executing SELECT command current information in the table will reflect on the screen.
3. The SELECT statement has three basic components that is:
SELECT, FROM and WHERE
4. This command extract useful information and stored in a table.
Syntax :- SELECT column_list
FROM table_list
WHERE search_scale
5. SELECT, FROM and WHERE are keywords.
The SELECT keyword is used to specify a column that you want to
retrieve from the column-list.
The FROM clause is used to specify the table from which the column is
to be retrieved (restore).
The WHERE clause is used to limit the rows and returned by your query.
SELECT * FROM table_name;
The (*) asterisk is used to fetch all the columns from the table.
Example :- Table students
Id | name | dob | class | section | address |
1781 | Diva | 12-09-1998 | 10 | A | 34K park street, Noida |
1790 | karan | 02-02-2001 | 8 | C | L 134 dreamworld sector12, Noida |
1765 | Vicky | 08-11-1999 | 10 | B | 23 / 89 jain vihar, Greater noida |
1796 | Harsh | 28--3-1999 | 7 | A | 78/90 B wings sector17, Greater noida |
SELECT id,name,class FROM students WHERE class=10;
This query will display three columns of the students table that is id, name, and the class whose class is 10.
Id | name | class |
1781 | Diva | 10 |
1765 | Vicky | 10 |
Example :-
SELECT * FROM students WHERE section='A';
This query will display five columns of the students table that is id, name, dob(date-of-birth), class, section and address whose section is A.
Id | name | dob | class | section | address |
1781 | Diva | 12-09-1998 | 10 | A | 34K park street, Noida |
1796 | Harsh | 28--3-1999 | 7 | A | 78/90 B wings sector17, Greater noida |
Use of WHERE clause
The WHERE clause is used to meet the certain condition from the table.It must follow the FROM clause. It compares the value in the columns, literal values, arithmetic expression and other functions.
It expects three elements that is:
A column name
A comparison operator such as =, >, <, != etc .
A column name, constant or list of values.
Syntax :-select-list
SELECT[ALL/DISTINCT] [TOPn] [PERCENT] [WITH TIES]]
[INTO new-Table]
[FROM table-Source]
[WHERE search_Condition]
[GROUP By [ALL] Group_by_expression
[WITH {CUBE ROLLUP}]]
[HAVING search_Condition]
[ORDER BY {column_name [Asc /DESC]} [,.........n]]
[COMPUTE {Column_Name [Asc /DESC]} [,.........n]]
[COMPUTE {{ AVG | COUNT | MAX | MIN | SUM } (expression) } [,....n]
[FOR BROWSE] [OPTION (query_hint[,.....n])]