Sql commands


Published on

Published in: Education, Technology
  • Be the first to comment

Sql commands

  1. 1. SQL QUERIES 1.CREATE TABLE STATEMENT: The create table statement is used to create the Database and also the tables in the database.The tables are divided into rows and columns.The columns consist of the attributes of the database and the rows consist of the values for these attributes. SYNTAX: CREATE TABLE "table_name" ("column 1" "data_type_for_column_1", "column 2" "data_type_for_column_2", ... )
  2. 2. 2.ALTER TABLE STATEMENT Once a table is created in the database, there are many occasions where one may wish to change the structure of the table. For this purpose, the alter table statement is used. SYNTAX: ALTER TABLE "table_name" [alter specification] 3.THE INSERT STATEMENT The Sql insert into clause facilitates the process of inserting data into a SQL table. SYNTAX: 1.For inserting into one row
  3. 3. INSERT INTO "table_name" ("column1", "column2", ...) VALUES ("value1", "value2", ...) 2.For inserting into specified rows: INSERT INTO "table1" ("column1", "column2", ...) SELECT "column3", "column4", ... 4. THE SELECT STATEMENT The SQL select clause selects data from one or more database tables and/or views SYNTAX: SELECT "column_name" FROM "table_name" 5.THE WHERE CLAUSE The SQL WHERE clause works in conjunction with other SQL clauses like SELECT, INSERT and UPDATE to specify a search condition for these statements.
  4. 4. SYNTAX: SELECT "column_name" FROM "table_name" WHERE "condition" 5.THE DISTINCT STATEMENT If we only want to select each DISTINCT element then this is easy to accomplish in SQL. All we need to do is to add DISTINCT after SELECT. SYNTAX: SELECT DISTINCT "column_name" FROM "table_name" 6.THE UPDATE STATEMENT The SQL UPDATE clause serves to update data in database table.
  5. 5. SYNTAX: UPDATE Table1 SET Column1 = Value1, Column2 = Value2, … 7.THE DELETE STATEMENT The SQL DELETE clause is used to delete data from a database table. SYNTAX: DELETE FROM Table1 8.THE TRUNCATE STATEMENT The SQL TRUNCATE TABLE clause deletes all rows from a database table. SYNTAX:
  6. 6. TRUNCATE TABLE table name 9.THE ORDERBY CLAUSE The SQL ORDER BY clause defines in what order to return a data set retrieved with a SQL SELECT statement. SYNTAX: SELECT "column_name" FROM "table_name" [WHERE "condition"] ORDER BY "column_name" [ASC, DESC] 10.SQL AGGREGATE FUNC TIONS SQL aggregate functions are used to sum, count, get the average, get the minimum and get the maximum values from a column or from a sub-set of column values.
  7. 7. 11.SQL COUNT This allows us to COUNT up the number of row in a certain table. SYNTAX: SELECT COUNT("column_name") FROM "table_name" 12.SQL AVERAGE SQL uses the AVG() function to calculate the average of a column. SYNTAX: SELECT AVG("column_name") FROM "table_name" 13.SQL MINIMUM SQL uses the MIN function to find the maximum value in a column. SYNTAX: SELECT MIN("column_name") FROM "table_name"
  8. 8. 14.SQL MAXIMUM SQL uses the MAX function to find the maximum value in a column. SYNTAX: SELECT MAX("column_name") FROM "table_name" 15.SQL SUM The SUM function is used to calculate the total for a column. SYNTAX: SELECT SUM("column_name") FROM "table_name" 16.SQL Group BY STATEMENT The SQL GROUP BY clause is used along with the SQL aggregate functions and specifies the groups where selected rows are placed. WHEN one or more aggregate functions are presented in the SQL SELECT column list, the SQL GROUP BY clause calculates a summary value for each group.
  9. 9. SYNTAX: SELECT "column_name1", SUM("column_name2") FROM "table_name" GROUP BY "column_name1" 17.SQL HAVING STATEMENT The SQL HAVING keyword provides a search condition for a group or aggregate. The SQL HAVING clause works together with the SQL SELECT clause. The SQL HAVING clause is somewhat similar to the SQL WHERE clause, because it specifies a search condition. SYNTAX: SELECT "column_name1", SUM("column_name2") FROM "table_name" GROUP BY "column_name1" HAVING (arithmetic function condition)
  10. 10. 18.SQL JOIN STATEMENT The SQL JOIN clause selects data from two or more tables tied together by matching table columns. SYNTAX: SELECT column1 from table1 join table2 on Condition 19.SQL UNION The SQL UNION clause merges the results of two or more SELECT SQL queries into one result set. When using SQL UNION, all the SQL expressions participating in the UNION must have the same structure 20.SQL TRIM The TRIM function in SQL is used to remove specified prefix or suffix from a string. The most common pattern being removed is white spaces. This function is called differently in different databases:
  11. 11. SYNTAX: TRIM([[LOCATION] [remstr] FROM ] str) 21.SQL LENGTH The Length function in SQL is used to get the length of a string. This function is called differently for the different databases: SYNTAX: Length(str)