Uploaded by bhagyasri2208

SQL

advertisement
SQL
Copyright © 2013 TATA Consultancy Services Limited
TCS CONFIDENTIAL
1
SQL BASICS
SQL is a standard language for accessing and manipulating databases.
It stands for Structured Query Language. Users interact with database
systems through query languages.
DDL : It is Data Definition Language which consist of 'CREATE,
DROP, TRUNCATE, ALTER'
Command.
DML : It is Data Manipulation Language which consist of 'INSERT,
DELETE, UPDATE' command.
DRL : It is Data Retrival Language which consist of 'SELECT'
command.
2
SQL BASICS
CREATE TABLE Statement:
The create statement is used to create a table in a database. Tables are
organized into rows and columns; and each table must have a name.
Syntax
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
....
);
DROP TABLE Statement:
The DROP TABLE statement is used to delete a table.
DROP TABLE table_name;
3
SQL BASICS
TRUNCATE TABLE Statement
It is used to delete whole table content and not the table framework.
TRUNCATE TABLE table_name;
The ALTER TABLE Statement
The alter table statement is used to add, delete, or modify columns in an
existing table.
Syntax
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype;
4
SQL BASICS
SELECT Statement
The select statement is used to select data from a database.
Syntax
SELECT column_name,column_name
FROM table_name;
OR
SELECT * FROM table_name;
INSERT Statement
The insert into statement is used to insert new records in a table.
Syntax
INSERT INTO table_name
VALUES (value1,value2,value3,...);
OR
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
5
SQL BASICS
UPDATE Statement
The update statement is used to update existing records in a table.
UPDATE table_name
SET column1 = value1,column2 = value2,...
WHERE some_column = some_value;
DELETE Statement
The delete statement is used to delete existing records in a table.
DELETE FROM table_name
WHERE some_column=some_value;
6
SQL JOIN
An SQL JOIN clause is used to combine rows from two or more
tables,based on a common field between them.
Inner Join:
Returns all rows when there is at least one match in BOTH tables.
select column_name(s)
from table1
Inner join table2
ON table1.column_name=table2.column_name;
7
SQL JOIN
Left Join:
Return all rows from the left table, and the matched rows from the right
table. The LEFT JOIN keyword returns all rows from the left table
(table1), with the matching rows in the right table (table2).
Right Join:
Return all rows from the right table, and the matched rows from the left
table. The RIGHT JOIN keyword returns all rows from the right table
(table2), with the matching rows in the left table (table1).
8
SQL CONSTRAINT
SQL constraints are used to specify rules for the data in a table.

NOT NULL - Indicates that a column cannot store NULL
value.

UNIQUE - Ensures that each row for a column must have a
unique
value.
PRIMARY KEY - A combination of a NOT NULL and UNIQUE.

FOREIGN KEY -It points to a primary key in another table.



CHECK- Ensures that the value in a column meets a specific
condition.
DEFAULT - Specifies a default value when specified none for
9
SQL CLAUSES
GROUP BY
The GROUP BY statement is used in conjunction with the aggregate
functions to group the result-set by one or more columns.
Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
10
SQL CLAUSES
HAVING
The HAVING clause was added to SQL because the WHERE keyword
could not be used with aggregate functions.
Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value;
11
SQL FUNCTIONS
SQL has many built-in functions for performing calculations on data.
SQL Aggregate Functions : 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
MAX() - Returns the largest value
MIN() - Returns the smallest value
SUM() - Returns the sum
12
Thank You
Copyright © 2013 TATA Consultancy Services Limited
13
TCS CONFIDENTIAL
Download