SQL Query Updated
SQL Query Updated
DDL - DDL is abbreviation of Data Definition Language. It is used to create and modify the structure of
database objects in database.
Examples: CREATE, ALTER, DROP statements
DML - DML is abbreviation of Data Manipulation Language. It is used to retrieve, store, modify, delete,
insert and update data in database.
Examples: SELECT, UPDATE, INSERT statements
DCL - DCL is abbreviation of Data Control Language. It is used to create roles, permissions, and
referential integrity as well it is used to control access to database by securing it.
Examples: GRANT, REVOKE statements
TCL - TCL is abbreviation of Transactional Control Language. It is used to manage different transactions
occurring within a database.
Examples: COMMIT, ROLLBACK statements
Data Manipulation Language (DML)
The SQL INSERT INTO clause is used to insert data into a SQL table. The SQL INSERT INTO is frequently
used and has the following generic syntax:
The SQL INSERT INTO clause has actually two parts - the first specifying the table we are inserting into
and giving the list of columns we are inserting values for, and the second specifying the values inserted in
the column list from the first part.
SQL - Insert
The SQL DELETE Query is used to delete the existing records from a table.
You can use WHERE clause with DELETE query to delete selected rows, otherwise all the records would be
deleted.
Removes rows from a table
Delete certain rows
DELETE FROM CUSTOMER_T WHERE STATE = ‘HI’;
Delete all rows
DELETE FROM CUSTOMER_T;
SQL - Drop
The SQL DROP TABLE statement is used to remove a table definition and all data, indexes, triggers,
constraints, and permission specifications for that table.
NOTE: You have to be careful while using this command because once a table is deleted then all the
information available in the table would also be lost forever.
The SQL TRUNCATE TABLE command is used to delete complete data from an existing table.
You can also use DROP TABLE command to delete complete table but it would remove complete table
structure form the database and you would need to re-create this table once again if you wish you store some
data.
SQL - Update
The SQL UPDATE command is used to modify data stored in database tables.
Modifies data in existing rows
UPDATE PRODUCT_T
SET UNIT_PRICE = 775
WHERE PRODUCT_ID = 7;
SQL - Select
SQL SELECT is without a doubt the most frequently used SQL command that's why we are starting our
tutorial with it. The SQL SELECT command is used to retrieve data from one or more database tables.
Clauses of the SELECT statement:
SELECT - List the columns (and expressions) that should be returned from the query
FROM - Indicate the table(s) or view(s) from which data will be obtained
WHERE - Indicate the conditions under which a row will be included in the result
GROUP BY - Indicate columns to group the results
HAVING - Indicate the conditions under which a group will be included
ORDER BY - Sorts the result according to specified columns
SQL Statement processing order
SQL Comparison Operators
Operator Meaning
= Equal To
> Greater Than
>= Greater Than or Equal To
< Less Than
<= Less Than or Equal To
<> Not Equal To
!= Not Equal To
SQL Aliases
SQL aliases are used to give a database table, or a column in a table, a temporary name.
Basically aliases are created to make column names more readable.
SQL aggregate functions return a single value, calculated from values in a column.
Useful aggregate functions:
AVG() - Returns the average value
COUNT() - Returns the number of rows
FIRST() - Returns the first value
LAST() - Returns the last value
MAX() - Returns the largest value
MIN() - Returns the smallest value
SUM() - Returns the sum
SQL Aggregate Functions
AND, OR, and NOT Operators for customizing conditions in WHERE clause
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.
SELECT PRODUCT_DESCRIPTION, PRODUCT_FINISH, STANDARD_PRICE
FROM PRODUCT_V
WHERE (PRODUCT_DESCRIPTION LIKE ‘%Desk’
OR PRODUCT_DESCRIPTION LIKE ‘%Table’)
AND UNIT_PRICE > 300;
SQL Between Operator
The BETWEEN operator selects values within a range. The values can be numbers, text, or dates.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
SQL Wildcards
In SQL, wildcard characters are used with the SQL LIKE operator.
SQL wildcards are used to search for data within a table.
With SQL, the wildcards are:
Wildcard Description
% A substitute for zero or more characters
_ A substitute for a single character
[charlist] Sets and ranges of characters to match
[^charlist]
or Matches only a character NOT specified within the brackets
[!charlist]
SQL Order Clause
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.
SELECT CUSTOMER_NAME, CITY, STATE
FROM CUSTOMER_V
WHERE STATE IN (‘FL’, ‘TX’, ‘CA’, ‘HI’)
ORDER BY STATE, CUSTOMER_NAME;
SQL Group Clause
Categorizing results
Like a WHERE clause, but it operates on groups (categories), not on individual rows. Here, only those
groups with total numbers greater than 1 will be included in final result
SQL Unions
The SQL UNION clause/operator is used to combine the results of two or more SELECT statements without
returning any duplicate rows.
To use UNION, each SELECT must have the same number of columns selected, the same number of column
expressions, the same data type, and have them in the same order, but they do not have to be the same
length.
The SQL DISTINCT keyword is used in conjunction with SELECT statement to eliminate all the duplicate
records and fetching only unique records.
There may be a situation when you have multiple duplicate records in a table. While fetching such records, it
makes more sense to fetch only unique records instead of fetching duplicate records.
The Structured Query Language (SQL) defines a standard for interacting with relational databases
Most platforms support ANSI-SQL 92
Most platforms provide many non-ANSI-SQL additions
Most important data modification SQL statements:
SELECT: Returning rows
UPDATE: Modifying existing rows
INSERT: Creating new rows
DELETE: Removing existing rows