Chapter 1 SQL
Chapter 1 SQL
It allows users to access the data and retrieve specific data based on
specific conditions.
The SELECT clause is the first clause and is one of the last clauses of
the select statement that the database server evaluates.
The syntax for the SELECT statement is:
Output:
Fetch All Table using SELECT Statement
Output:
SELECT Statement with WHERE Clause
Output:
SQL SELECT Statement with GROUP BY Clause
Output:
SELECT Statement with HAVING Clause
Query:
Output:
SELECT Statement with ORDER BY clause in SQL
Here,
Query:
?
Select the first 3 records of the Customers table:
LIMIT SELECT * FROM Customers
LIMIT 3;
3 Antonio Moreno Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
Taquería
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
• It is used to access records from one or more database tables and views.
• The SELECT statement retrieves selected data based on specified conditions.
• The result of a SELECT statement is stored in a result set or result table.
• The SELECT statement can be used to access specific columns or all columns from a
table.
• It can be combined with clauses like WHERE, GROUP BY, HAVING, and ORDER BY
for more refined data retrieval.
• The SELECT statement is versatile and allows users to fetch data based on various
criteria efficiently.
SQL Views
• Views in SQL are a kind of virtual table. A view also has rows and columns like
tables, but a view doesn’t store data on the disk like a table. View defines a customized
query that retrieves data from one or more tables, and represents the data as if it was
coming from a single source.
• We can create a view by selecting fields from one or more tables present in the
database. A View can either have all the rows of a table or specific rows based on
certain conditions.
Create StudentDetails table:
StudentMarks:
SELECT * FROM DetailsView;
In this example, we will create a View named DetailsView from the table
StudentDetails.
OUTPUT:
Example 2: Create View From Table
In this example, we will create a view named StudentNames from the table
StudentDetails.
Output:
CREATE VIEW StudentNames AS
SELECT S_ID, NAME
FROM StudentDetails
ORDER BY NAME;
• In this example we will create a View named MarksView from two tables
StudentDetails and StudentMarks. To create a View from multiple tables we
can simply include multiple tables in the SELECT statement.
Output:
UPDATE VIEW in SQL
• If you want to update the existing data within the view, use the UPDATE
statement.
UPDATE view_name
Syntax: SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
Note: Not all views can be updated using the UPDATE statement.
If you want to update the view definition without affecting the data, use
the CREATE OR REPLACE VIEW statement.
CREATE OR REPLACE VIEW view_name AS
Syntax: SELECT column1, column2, ...
FROM table_name
WHERE condition;
Rules to Update Views in SQL:
Certain conditions need to be satisfied to update a view. If any of these conditions are not met, the
view can not be updated.
1.The SELECT statement which is used to create the view should not include GROUP BY clause or
ORDER BY clause.
2.The SELECT statement should not have the DISTINCT keyword.
3.The View should have all NOT NULL values.
4.The view should not be created using nested queries or complex queries.
5.The view should be created from a single table. If the view is created using multiple tables then
we will not be allowed to update the view.
Example 1: Update View to Add or Replace a View Field
Output:
Example 2: Update View to Insert a row in a view
Output:
DELETE VIEWS in SQL