0% found this document useful (0 votes)
24 views91 pages

SQL (1) - 241105 - 150950

Uploaded by

veena shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views91 pages

SQL (1) - 241105 - 150950

Uploaded by

veena shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 91

SQL

SQL (Structured Query Language) is used to perform


operations on the records stored in the database,
such as updating records, inserting records, deleting
records, creating and modifying database tables,
views, etc.
SQL is not a database system, but it is a query
language.
SQL Syntax
SQL is followed by unique set of rules and guidelines called
Syntax.

All the SQL statements start with any of the keywords like
SELECT, INSERT, UPDATE, DELETE, ALTER, DROP, CREATE and
all the statements end with a semicolon (;).

Important point to be noted is that SQL is case insensitive,


which means SELECT and select have same meaning in SQL
statements.
Data Type Description
INT Stores numeric values in the range of -
2147483648 to 2147483647

DECIMAL Stores decimal values with exact


precision.
CHAR Stores fixed-length strings with a
maximum size of 255 characters.

VARCHAR Stores variable-length strings with a


maximum size of 65,535 characters.

DATE Stores date values in the YYYY-MM-DD


format.
SQL CREATE TABLE Statement
Creating a Table
Create some tables inside our database that will actually hold the data. A
database table simply organizes the information into rows and columns.
The SQL CREATE TABLE statement is used to create a table.
Syntax
The basic syntax for creating a table can be given with:
CREATE TABLE table_name ( column1_name data_type constraints,
column2_name data_type constraints, .... );
SQL INSERT Statement

Inserting Data in Table


we've created a table named persons. Now it's time to insert some data
inside our newly created database table.
The INSERT INTO statement is used to insert new rows in a database
table.

Syntax
The basic syntax for inserting data into a table can be given with:

INSERT INTO table_name (column1,column2,...) VALUES (value1,val


ue2,...);

Here the column1, column2,..., etc. represents the name of the table
columns, whereas the value1, value2,..., and so on represents the
corresponding values for these columns.
Adding Records to a Table
INSERT INTO persons (name, birth_date, phone) VALUES ('Peter
Wilson', '1990-07-15', '0711-020361');

SQL SELECT Statement

Selecting Data from Table


we've learned how to insert data in a database table.
Now it's time to select the data from existing tables using the SQL query.

The SELECT statement is used to select or retrieve the data from one or
more tables.
You can use this statement to retrieve all the rows from a table in one go,
as well as to retrieve only those rows that satisfy a certain condition or a
combination of conditions.
Syntax
The basic syntax for selecting the data from a table can be given with:

SELECT column1_name, column2_name, columnN_name


FROM table_name;

Here, column1_name, column2_name, ... are the names of the columns or


fields of a database table whose values you want to fetch.

However, if you want to fetch the values of all the columns available in a
table, you can just use the following syntax:

Select All from Table

SELECT * FROM table_name;


SQL WHERE Clause
Selecting Record Based on Condition

Syntax
The WHERE clause is used with the SELECT statement to extract only
those records that fulfill specified conditions. The basic syntax can be
given with:

SELECT column_list FROM table_name WHERE condition;0


if you want to fetch the values of all the columns available in a table,
you can use the following syntax:

SELECT * FROM table_name WHERE condition;


Filter Records with WHERE Clause
The following SQL statement will returns all the employees from the employees table,
whose salary is greater than 7000. The WHERE clause simply filtered out the unwanted
data.
SELECT * FROM employees WHERE
salary > 7000;
As you can see the output contains only those employees whose salary is
greater than 7000. Similarly, you can fetch records from specific
columns, like this:

SELECT emp_id, emp_name,


hire_date, salary FROM employees
WHERE salary > 7000;

The following statement will fetch the records of an employee whose


employee id is 2.

SELECT * FROM employees WHERE


emp_id = 2;
SQL IN & BETWEEN Operators
SQL ORDER BY Clause
SQL DISTINCT Clause
SQL UPDATE Statement
SQL DELETE Statement
Delete is a DML Command
SQL TRUNCATE TABLE Statement Truncate is a DDL Command

The following command removes all the rows from the employees table:

TRUNCATE TABLE employees;


SQL DROP Statement Drop is a DDL Command

DROP TABLE persons;

Similarly, you can delete a database using the DROP DATABASE statement.
SQL Joining Tables
SQL INNER JOIN Operation
SELECT t1.emp_id, t1.emp_name, t1.hire_date, t2.dept_name
FROM employees AS t1 INNER JOIN departments AS t2
ON t1.dept_id = t2.dept_id ORDER BY emp_id;
SQL LEFT JOIN Operation
SQL RIGHT JOIN Operation
SQL FULL JOIN Statement
SQL CROSS JOIN Operation
The UNION operation eliminates the duplicate rows from the combined
result set, by default. That's why the above query returns only 9 rows,
because if you notice the name "Martin Blank" appears in both the
employees and customers tables.

However, if you want to keep the duplicate rows you can use the ALL
keyword, as follow:
SQL LIKE Operator
SQL ALTER TABLE Statement
Adding Constraints
SQL Aliases
SQL GROUP BY Clause
SQL HAVING Clause
SQL CREATE VIEW Statement
Suppose that you want retrieve the id and name of the employees
along with their department name
//we can execute either using Execute statement or
B using PL/SQL Block

//Execute statement

//PL/SQL block
Example

You might also like