Monday, January 4, 2010

SQL Keywords

SQL keywords are case insensitive. So the keyword SELECT and select will behave in the same way. Let’s look at some of the commonly used SQL keywords.
  • AND : AND is an sql operator which is used for adding multiple condition. If the conditions are satisfied by a row. It will be part of the result set.
  • SyntaxSELECT column_name FROM table_name WHERE column1 = value1 AND column2 = value2;
    Output On execution of this command, if column1 is equal to value1 and column2 is equal to value2 then only the row will be qualified for result set

  • AS : AS is used for aliasing a column or table.
  • SyntaxSELECT AVG(column_name) FROM table_name;
    Output Execution of this command will provide the average of column_name.

  • BETWEEN : BETWEEN is an operator which is used for defining a set. The value for range can be numeric, text and date.
  • SyntaxSELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;
    Output Execution of this command will give a result set bounded by value1 and value2.

  • COUNT : COUNT is a function which provides the count of the row for a column. The column should be a not null column.
  • SyntaxSELECT COUNT(column_name) FROM table_name;
    Output On execution of this command, the result will contain the count of the rows for column_name.

  • CREATE TABLE : This command is used to create table in the database. It allows to specify the name of the table along with the name a datatype of the column.
  • SyntaxCREATE TABLE table_name (column1 datatype, column2 datatype);
    Output Execution of this command will result in creation of table with name as table_name along with column1 and column2 as the columns of the table.

  • DELETE : DELETE is the command which is used for removing rows from the table.
  • SyntaxDELETE FROM table_name WHERE column_name = value;
    Output On execution of this command the rows that will be selected based on the condition in the WHERE clause will be deleted.

  • GROUP BY : GROUP BY is a clause in SQL that is only used with aggregate functions. It is used along with the SELECT statement to arrange identical data into groups.
  • SyntaxSELECT COUNT(*) FROM table_name GROUP BY column_name;
    Output Execution of this order will result in grouping the result set based on the column_name.

  • INNER JOIN : INNER JOIN will select records with matching values in two tables.
  • SyntaxSELECT column_name FROM table_1 INNER JOIN table_2 ON table_1.column_name = table_2.column_name;
    Output Above command will result in rows where both tables have matching values for column_name.

  • INSERT : INSERT is used for adding a new row in the table.
  • SyntaxINSERT INTO table_name (column1, column2, column3) VALUES (value1, 'value2', value3);
    Output Execution of this command will result in addition of a new row with values corresponding to the columns.

  • LIKE : LIKE is an operator which is used for specifying a pattern. This operator is used along with WHERE clause.
  • SyntaxSELECT column_name FROM table_name WHERE column_name LIKE pattern;
    Output Output will the rows that will satisfy the like pattern.

  • LIMIT : LIMIT is a clause which allows the restricting the result set rows to maximum number specified by the limit clause.
  • SyntaxSELECT column_name FROM table_name LIMIT number;
    Output The resultset will be limited by the number that is provided as part of the limit clause.

  • MAX : MAX is a function that will return the max value from the column specified.
  • SyntaxSELECT MAX(column_name) FROM table_name;
    Output The output will the maximum value specified in the column column_name.

  • MIN : MIN is a function that will return the min value from the column specified.
  • SyntaxSELECT MIN(column_name) FROM table_name;
    Output The output will the minimum value specified in the column column_name.

  • OR : OR is the operator that is used for selecting the rows based on satisfaction of either condition in WHERE clause.
  • SyntaxSELECT column_name FROM table_name WHERE column_name = value1 OR column_name = value2;
    Output Result set will contain rows where column_name value will be either value1 or value2.

  • ORDER BY : ORDER BY is used for sorting of columns in ascending or descending order numerically or in alphabetical order.
  • SyntaxSELECT column_name FROM table_name ORDER BY column_name ASC;
    Output On execution of the command above we will get the result set in ascending order.

  • LEFT JOIN, RIGHT JOIN : These will combine rows from different tables even if the join condition is not met. Every row in the left/right table is returned in the result set, and if the join condition is not met, then NULL values are filled in the columns from the left/right table.
  • SyntaxSELECT column_name(s) FROM table_1 LEFT JOIN table_2 ON table_1.column_name = table_2.column_name;
    Output Execution of the command above will result in rows from the table_1 along with rows which satisfies the condition from table_2
    SyntaxSELECT column_name(s) FROM table_1 RIGHT JOIN table_2 ON table_1.column_name = table_2.column_name;
    Output Execution of the command above will result in rows from the table_2 along with rows which satisfies the condition from table_1.

  • ROUND : ROUND is a function that rounds of the number specified in the column based on the integer that is specified as part of the function.
  • SyntaxSELECT ROUND(column_name, integer) FROM table_name;
    Output The output of the command will result in rounding up the number based on the integer that is provided as part of the function.

  • SELECT : SELECT is used to fetch the data from the data base.
  • SyntaxSELECT column_name FROM table_name
    Output On execution of this command the result set will contain rows for column column_name.

  • SELECT DISTINCT : SELECT DISTINCT is used for retrieving distinct values from the column that is specified.
  • SyntaxSELECT DISTINCT column_name FROM table_name;
    Output On execution of the command above the result set will only contain the unique values from the column column_name.

  • SUM : SUM is a function will provides the total value of a column which is numeric.
  • SyntaxSELECT SUM(column_name) FROM table_name;
    Output Execution of this command will result in the total of all the row that are part of the column column_name

  • UPDATE : UPDATE is used for updating values of a row of a table.
  • SyntaxUPDATE table_name SET some_column = some_value WHERE some_column = some_value;
    Output Execution of this command will result in updating the row that will satisfy the condition in the where clause.

  • WHERE : WHERE is used for specifying the condition that should be satisfied for selecting the row to be part of the result set.
  • SyntaxSELECT column_name FROM table_name WHERE column_name operator value;
    Output The output of this command will result in the rows that are satisfying the where clause.