SlideShare uma empresa Scribd logo
1 de 80
SQL
RDBMS Commands explain with Examples
What Is SQL ??
● SQL stands for Structured Query Language
● SQL lets you access and manipulate databases
● SQL became a standard of the American National Standards Institute (ANSI)
in 1986, and of the International Organization for Standardization (ISO) in
1987
What SQl can do ??
● SQL can execute queries against a database
● SQL can retrieve data from a database
● SQL can insert records in a database
● SQL can update records in a database
● SQL can delete records from a database
● SQL can create new databases
● SQL can create new tables in a database
● SQL can create stored procedures in a database
● SQL can create views in a database
● SQL can set permissions on tables, procedures, and views
RDBMS
● RDBMS stands for Relational Database Management System.
● RDBMS is the basis for SQL, and for all modern database systems such as MS
SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.
● The data in RDBMS is stored in database objects called tables. A table is a
collection of related data entries and it consists of columns and rows.
Select
For selected columns as output :- SELECT column1, column2, …
FROM table_name;
For print all the columns as output :- SELECT * FROM table_name;
For printing distinct value of column from table as output :-
SELECT DISTINCT column1, column2, …
FROM table_name;
Select - Example
For selected columns as output :- SELECT Country FROM Customers;
Select - Example
For selected columns as output :- SELECT * FROM Customers;
Select - Example
For selected columns as output :- SELECT DISTINCT Country FROM Customers;
Select - Example
Where
● The WHERE clause is used to filter records.
● The WHERE clause is used to extract only those records that fulfill a
specified condition.
● Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Command :- SELECT * FROM Customers
WHERE Country='Mexico';
where - Example
AND, OR ,NOT
The WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one
condition:
● The AND operator displays a record if all the conditions separated by AND
is TRUE.
● The OR operator displays a record if any of the conditions separated by OR is
TRUE.
The NOT operator displays a record if the condition(s) is NOT TRUE.
AND, OR ,NOT
AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
AND, OR ,NOT - Example
AND Command :- SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
AND, OR ,NOT - Example
OR Command :- SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
AND, OR ,NOT - Example
OR Command :- SELECT * FROM Customers
WHERE NOT Country='Germany';
Order by
The ORDER BY keyword is used to sort the result-set in ascending or
descending order.
The ORDER BY keyword sorts the records in ascending order by default. To
sort the records in descending order, use the DESC keyword.
Syntax :-
SELECT column1, column2, …
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Order by - Example
ORDER BY Command : - SELECT * FROM Customers
ORDER BY Country;
Order by - Example
ORDER BY Command : - SELECT * FROM Customers
ORDER BY Country DESC;
INSERT INTO
It is possible to write the INSERT INTO statement in two ways.
The first way specifies both the column names and the values to be inserted:
Syntax 1 :-INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Syntax 2 :- INSERT INTO table_name
VALUES (value1, value2, value3, ...);
INSERT INTO - Example
INSERT INTO Command : - INSERT INTO Customers (CustomerName, ContactName,
Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
OUTPUT :- You have made changes to the database. Rows affected: 1
NULL VAlue
A field with a NULL value is a field with no value.
If a field in a table is optional, it is possible to insert a new record or update
record without adding a value to this field. Then, the field will be saved with
a NULL value.
Syntax 1 :-SELECT column_names
FROM table_name
WHERE column_name IS NULL;
Syntax 2 :- SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
NULL - Example
NULL Command : - SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NOT NULL;
OUTPUT :-
update
The UPDATE statement is used to modify the existing records in a table.
Syntax :- UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
INSERT INTO - Example
INSERT INTO Command : - UPDATE Customers
SET ContactName='Alfred Schmidt', City='Frankfurt'
WHERE CustomerID=1;
OUTPUT :- You have made changes to the database. Rows affected: 1
Delete
The DELETE statement is used to delete existing records in a table.
Syntax :- DELETE FROM table_name WHERE condition;
To DELETE all the records.
Syntax :- DELETE FROM table_name;
Delete - Example
Command : - DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
OUTPUT :- You have made changes to the database. Rows affected: 1
TOP, LIMIT or ROWNUM Clause
The SELECT TOP clause is used to specify the number of records to return.
The SELECT TOP clause is useful on large tables with thousands of records.
Returning a large number of records can impact on performance.
Note: Not all database systems support the SELECT TOP clause. MySQL
supports the LIMIT clause to select a limited number of records, while
Oracle uses ROWNUM.
TOP, LIMIT or ROWNUM Clause
SQL Server / MS Access Syntax :- SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
MySQL Syntax : -SELECT column_name(s) FROM table_name
WHERE condition
LIMIT number;
Oracle Syntax :- SELECT column_name(s) FROM table_name
WHERE ROWNUM <= number;
TOP - Example
Command : - SELECT TOP 3 * FROM Customers;
OUTPUT :-
LImit - Example
Command : - SELECT * FROM Customers LIMIT 3;
OUTPUT :-
rownum - Example
Command : - SELECT * FROM Customers
WHERE ROWNUM <= 3;
OUTPUT :-
Min & max
The MAX() function returns the largest value of the selected column.
Syntax :- SELECT MIN(column_name)
FROM table_name
WHERE condition;
The MIN() function returns the smallest value of the selected column.
Syntax :- SELECT MAX(column_name)
FROM table_name
WHERE condition;
Min - Example
Command : - SELECT MIN(Price) AS SmallestPrice
FROM Products;
OUTPUT :-
MAX - Example
Command : - SELECT MAX(Price) AS LargestPrice
FROM Products;
OUTPUT :-
Count, avg, sum
The COUNT() function returns the number of rows that matches a specified
criteria.
Syntax :- SELECT COUNT(column_name)
FROM table_name
WHERE condition;
count - Example
Command : - SELECT COUNT(ProductID)
FROM Products;
OUTPUT :-
Count, avg, sum
The AVG() function returns the average value of a numeric column.
Syntax:- SELECT AVG(column_name)
FROM table_name
WHERE condition;
AVG- Example
Command : - SELECT AVG(Price)
FROM Products;
OUTPUT :-
Count, avg, sum
The SUM() function returns the total sum of a numeric column.
Syntax:- SELECT SUM(column_name)
FROM table_name
WHERE condition;
sum - Example
Command : - SELECT COUNT(ProductID)
FROM Products;
OUTPUT :-
Like
The LIKE operator is used in a WHERE clause to search for a specified pattern in
a column.
There are two wildcards used in conjunction with the LIKE operator:
● % - The percent sign represents zero, one, or multiple characters
● _ - The underscore represents a single character
Syntax :- SELECT column1, column2, …
FROM table_name
WHERE columnN LIKE pattern;
like - Example
The following SQL statement selects all customers with a CustomerName
starting with "a":
Command : - SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';
OUTPUT :-
like - Example
The following SQL statement selects all customers with a ContactName that
starts with "a" and ends with "o":
Command : - SELECT * FROM Customers
WHERE ContactName LIKE 'a%o';
OUTPUT :-
like - Example
The following SQL statement selects all customers with a CustomerName that
does NOT start with "a":
Command : - SELECT * FROM Customers
WHERE CustomerName NOT LIKE 'a%';
OUTPUT :-
wildcard
A wildcard character is used to substitute any other character(s) in a string.
Wildcard characters are used with the SQL LIKE operator. The LIKE operator is
used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards used in conjunction with the LIKE operator:
% - The percent sign represents zero, one, or multiple characters
_ - The underscore represents a single character
wildcard
Here are some examples showing different LIKE operators with '%' and '_'
wildcards:
wildcard - Example
Command : - SELECT * FROM Customers
WHERE City LIKE 'ber%';
OUTPUT :-
IN
The IN operator allows you to specify multiple values in a WHERE clause.
The IN operator is a shorthand for multiple OR conditions.
Syntax 1 :- SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
Syntax 2 :- SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
in - Example
Command : - SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
OUTPUT :-
between
The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.
Syntax :- SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
between - Example
Command : - SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
OUTPUT :-
aliases
SQL aliases are used to give a table, or a column in a table, a temporary name.
Aliases are often used to make column names more readable.
An alias only exists for the duration of the query.
Column Syntax :- SELECT column_name AS alias_name
FROM table_name;
Table Syntax :- SELECT column_name(s)
FROM table_name AS alias_name;
aliases for column- Example
Command : - SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;
OUTPUT :-
aliases for table- Example
Command : - SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Customers AS c, Orders AS o
WHERE c.CustomerName="Around the Horn" AND c.CustomerID=o.CustomerID;
OUTPUT :-
joins
A JOIN clause is used to combine rows from two or more tables, based on a
related column between them.
Different Types of Joins :- i. Inner Join
ii. Left Join
iii. Right Join
iv. Full Join
v. Self Join
joins
i. Inner Join : - The INNER JOIN keyword selects records that have matching
values in both tables.
Syntax : - SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
joins
ii. Left Join : - The LEFT JOIN keyword returns all records from the left table
(table1), and the matched records from the right table (table2). The result is NULL
from the right side, if there is no match.
Syntax : - SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
joins
iii. Right Join : - The RIGHT JOIN keyword returns all records from the right
table (table2), and the matched records from the left table (table1). The result is
NULL from the left side, when there is no match.
Syntax : - SELECT column_name(s)
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;
joins
iv. Full Join : - The FULL OUTER JOIN keyword return all records when there is
a match in either left (table1) or right (table2) table records.
Note: FULL OUTER JOIN can potentially return very large result-sets!
Syntax : - SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;
joins
v. Self Join : - A self JOIN is a regular join, but the table is joined with itself.
Syntax : - SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
Union
The UNION operator is used to combine the result-set of two or more SELECT
statements.
● Each SELECT statement within UNION must have the same number of
columns
● The columns must also have similar data types
● The columns in each SELECT statement must also be in the same order
Union
Union Syntax : - SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
Union All Syntax : - SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
Union - Example
Union - Example
Command :- SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
Union - Example (All)
Command:- SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;
Union - Example with where
Command :- SELECT City, Country FROM Customers WHERE Country='Germany'
UNION
SELECT City, Country FROM Suppliers WHERE Country='Germany'
ORDER BY City;
Group By
The GROUP BY statement is often used with aggregate functions (COUNT,
MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
Syntax :- SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Group by - Example
Command :- SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
Having
The HAVING clause was added to SQL because the WHERE keyword could not
be used with aggregate functions.
Syntax :- SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Having - Example
Command :- SELECT COUNT(CustomerID), Country FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
Exists
The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns true if the subquery returns one or more records.
Syntax :- SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
Exists - Example
Command :- SELECT SupplierName FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE SupplierId = Suppliers.supplierId AND
Price < 20);
Any & all
The ANY and ALL operators are used with a WHERE or HAVING clause.
The ANY operator returns true if any of the subquery values meet the condition.
The ALL operator returns true if all of the subquery values meet the condition.
ANY Syntax :- SELECT column_name(s) FROM table_name
WHERE column_name operator ANY
(SELECT column_name FROM table_name WHERE condition);
ALL Syntax :- SELECT column_name(s) FROM table_name
WHERE column_name operator ALL
(SELECT column_name FROM table_name WHERE condition);
Any & all- Example
Command :- SELECT ProductName FROM Products
WHERE ProductID = ANY (SELECT ProductID FROM OrderDetails WHERE Quantity =
10);
Any & all- Example
Command :- SELECT ProductName
FROM Products
WHERE ProductID = ALL (SELECT ProductID FROM OrderDetails WHERE Quantity =
10);
Select into
The SELECT INTO statement copies data from one table into a new table.
ALL Column Syntax :- SELECT * INTO newtable [IN externaldb] FROM oldtable
WHERE condition;
Selected Column Syntax :- SELECT column1, column2, column3, …
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;
Insert into
The INSERT INTO SELECT statement copies data from one table and inserts it
into another table.
INSERT INTO SELECT requires that data types in source and target tables
match
The existing records in the target table are unaffected
Insert into
Copy all columns Syntax :- INSERT INTO table2
SELECT * FROM table1
WHERE condition;
Copy only some columns Syntax :- INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, …
FROM table1
WHERE condition;
Comments
Single line comments start with --.Any text between -- and the end of the line will
be ignored (will not be executed).
The following example uses a single-line comment as an explanation:
Syntax :- --Select all:
SELECT * FROM Customers;
Multi-line comments start with /* and end with */.Any text between /* and */ will
be ignored.
Syntax :- /*Select all the columns of all the records in the Customers table:*/
SELECT * FROM Customers;

Mais conteúdo relacionado

Mais procurados (20)

SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
SQL
SQLSQL
SQL
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)
 
SQL
SQLSQL
SQL
 
Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
SQL
SQLSQL
SQL
 
Introduction to structured query language (sql)
Introduction to structured query language (sql)Introduction to structured query language (sql)
Introduction to structured query language (sql)
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
Sql and Sql commands
Sql and Sql commandsSql and Sql commands
Sql and Sql commands
 
Mysql joins
Mysql joinsMysql joins
Mysql joins
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sql
 
Chapter 4 Structured Query Language
Chapter 4 Structured Query LanguageChapter 4 Structured Query Language
Chapter 4 Structured Query Language
 
SQL Join Basic
SQL Join BasicSQL Join Basic
SQL Join Basic
 
Sql commands
Sql commandsSql commands
Sql commands
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQL
 
SQL
SQLSQL
SQL
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 

Semelhante a Sql (20)

Introduction to structured query language
Introduction to structured query languageIntroduction to structured query language
Introduction to structured query language
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
ADV Powepoint 2 Lec.pptx
ADV Powepoint 2 Lec.pptxADV Powepoint 2 Lec.pptx
ADV Powepoint 2 Lec.pptx
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
 
SQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdfSQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdf
 
Query
QueryQuery
Query
 
Introduction to sql new
Introduction to sql newIntroduction to sql new
Introduction to sql new
 
Sql
SqlSql
Sql
 
SQL
SQLSQL
SQL
 
SQL
SQLSQL
SQL
 
2.2 sql commands
2.2 sql commands2.2 sql commands
2.2 sql commands
 
Complete Sql Server querries
Complete Sql Server querriesComplete Sql Server querries
Complete Sql Server querries
 
Sql 2006
Sql 2006Sql 2006
Sql 2006
 
Dbms
DbmsDbms
Dbms
 
Hira
HiraHira
Hira
 
Select To Order By
Select  To  Order BySelect  To  Order By
Select To Order By
 
ADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptxADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptx
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 

Mais de Aman Lalpuria

Mais de Aman Lalpuria (6)

SWIFT - Clearing and Settlement
SWIFT - Clearing and Settlement SWIFT - Clearing and Settlement
SWIFT - Clearing and Settlement
 
Functors, applicatives &amp; monads
Functors, applicatives &amp; monadsFunctors, applicatives &amp; monads
Functors, applicatives &amp; monads
 
iPython
iPythoniPython
iPython
 
Git & Github
Git & GithubGit & Github
Git & Github
 
Sql basics
Sql basicsSql basics
Sql basics
 
Payment
PaymentPayment
Payment
 

Último

Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 

Último (20)

Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 

Sql

  • 2. What Is SQL ?? ● SQL stands for Structured Query Language ● SQL lets you access and manipulate databases ● SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standardization (ISO) in 1987
  • 3. What SQl can do ?? ● SQL can execute queries against a database ● SQL can retrieve data from a database ● SQL can insert records in a database ● SQL can update records in a database ● SQL can delete records from a database ● SQL can create new databases ● SQL can create new tables in a database ● SQL can create stored procedures in a database ● SQL can create views in a database ● SQL can set permissions on tables, procedures, and views
  • 4. RDBMS ● RDBMS stands for Relational Database Management System. ● RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access. ● The data in RDBMS is stored in database objects called tables. A table is a collection of related data entries and it consists of columns and rows.
  • 5. Select For selected columns as output :- SELECT column1, column2, … FROM table_name; For print all the columns as output :- SELECT * FROM table_name; For printing distinct value of column from table as output :- SELECT DISTINCT column1, column2, … FROM table_name;
  • 7. For selected columns as output :- SELECT Country FROM Customers; Select - Example
  • 8. For selected columns as output :- SELECT * FROM Customers; Select - Example
  • 9. For selected columns as output :- SELECT DISTINCT Country FROM Customers; Select - Example
  • 10. Where ● The WHERE clause is used to filter records. ● The WHERE clause is used to extract only those records that fulfill a specified condition. ● Syntax SELECT column1, column2, ... FROM table_name WHERE condition;
  • 11. Command :- SELECT * FROM Customers WHERE Country='Mexico'; where - Example
  • 12. AND, OR ,NOT The WHERE clause can be combined with AND, OR, and NOT operators. The AND and OR operators are used to filter records based on more than one condition: ● The AND operator displays a record if all the conditions separated by AND is TRUE. ● The OR operator displays a record if any of the conditions separated by OR is TRUE. The NOT operator displays a record if the condition(s) is NOT TRUE.
  • 13. AND, OR ,NOT AND Syntax SELECT column1, column2, ... FROM table_name WHERE condition1 AND condition2 AND condition3 ...; OR Syntax SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 OR condition3 ...; NOT Syntax SELECT column1, column2, ... FROM table_name WHERE NOT condition;
  • 14. AND, OR ,NOT - Example AND Command :- SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin';
  • 15. AND, OR ,NOT - Example OR Command :- SELECT * FROM Customers WHERE City='Berlin' OR City='München';
  • 16. AND, OR ,NOT - Example OR Command :- SELECT * FROM Customers WHERE NOT Country='Germany';
  • 17. Order by The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword. Syntax :- SELECT column1, column2, … FROM table_name ORDER BY column1, column2, ... ASC|DESC;
  • 18. Order by - Example ORDER BY Command : - SELECT * FROM Customers ORDER BY Country;
  • 19. Order by - Example ORDER BY Command : - SELECT * FROM Customers ORDER BY Country DESC;
  • 20. INSERT INTO It is possible to write the INSERT INTO statement in two ways. The first way specifies both the column names and the values to be inserted: Syntax 1 :-INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); Syntax 2 :- INSERT INTO table_name VALUES (value1, value2, value3, ...);
  • 21. INSERT INTO - Example INSERT INTO Command : - INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country) VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway'); OUTPUT :- You have made changes to the database. Rows affected: 1
  • 22. NULL VAlue A field with a NULL value is a field with no value. If a field in a table is optional, it is possible to insert a new record or update record without adding a value to this field. Then, the field will be saved with a NULL value. Syntax 1 :-SELECT column_names FROM table_name WHERE column_name IS NULL; Syntax 2 :- SELECT column_names FROM table_name WHERE column_name IS NOT NULL;
  • 23. NULL - Example NULL Command : - SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NOT NULL; OUTPUT :-
  • 24. update The UPDATE statement is used to modify the existing records in a table. Syntax :- UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
  • 25. INSERT INTO - Example INSERT INTO Command : - UPDATE Customers SET ContactName='Alfred Schmidt', City='Frankfurt' WHERE CustomerID=1; OUTPUT :- You have made changes to the database. Rows affected: 1
  • 26. Delete The DELETE statement is used to delete existing records in a table. Syntax :- DELETE FROM table_name WHERE condition; To DELETE all the records. Syntax :- DELETE FROM table_name;
  • 27. Delete - Example Command : - DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste'; OUTPUT :- You have made changes to the database. Rows affected: 1
  • 28. TOP, LIMIT or ROWNUM Clause The SELECT TOP clause is used to specify the number of records to return. The SELECT TOP clause is useful on large tables with thousands of records. Returning a large number of records can impact on performance. Note: Not all database systems support the SELECT TOP clause. MySQL supports the LIMIT clause to select a limited number of records, while Oracle uses ROWNUM.
  • 29. TOP, LIMIT or ROWNUM Clause SQL Server / MS Access Syntax :- SELECT TOP number|percent column_name(s) FROM table_name WHERE condition; MySQL Syntax : -SELECT column_name(s) FROM table_name WHERE condition LIMIT number; Oracle Syntax :- SELECT column_name(s) FROM table_name WHERE ROWNUM <= number;
  • 30. TOP - Example Command : - SELECT TOP 3 * FROM Customers; OUTPUT :-
  • 31. LImit - Example Command : - SELECT * FROM Customers LIMIT 3; OUTPUT :-
  • 32. rownum - Example Command : - SELECT * FROM Customers WHERE ROWNUM <= 3; OUTPUT :-
  • 33. Min & max The MAX() function returns the largest value of the selected column. Syntax :- SELECT MIN(column_name) FROM table_name WHERE condition; The MIN() function returns the smallest value of the selected column. Syntax :- SELECT MAX(column_name) FROM table_name WHERE condition;
  • 34. Min - Example Command : - SELECT MIN(Price) AS SmallestPrice FROM Products; OUTPUT :-
  • 35. MAX - Example Command : - SELECT MAX(Price) AS LargestPrice FROM Products; OUTPUT :-
  • 36. Count, avg, sum The COUNT() function returns the number of rows that matches a specified criteria. Syntax :- SELECT COUNT(column_name) FROM table_name WHERE condition;
  • 37. count - Example Command : - SELECT COUNT(ProductID) FROM Products; OUTPUT :-
  • 38. Count, avg, sum The AVG() function returns the average value of a numeric column. Syntax:- SELECT AVG(column_name) FROM table_name WHERE condition;
  • 39. AVG- Example Command : - SELECT AVG(Price) FROM Products; OUTPUT :-
  • 40. Count, avg, sum The SUM() function returns the total sum of a numeric column. Syntax:- SELECT SUM(column_name) FROM table_name WHERE condition;
  • 41. sum - Example Command : - SELECT COUNT(ProductID) FROM Products; OUTPUT :-
  • 42. Like The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards used in conjunction with the LIKE operator: ● % - The percent sign represents zero, one, or multiple characters ● _ - The underscore represents a single character Syntax :- SELECT column1, column2, … FROM table_name WHERE columnN LIKE pattern;
  • 43. like - Example The following SQL statement selects all customers with a CustomerName starting with "a": Command : - SELECT * FROM Customers WHERE CustomerName LIKE 'a%'; OUTPUT :-
  • 44. like - Example The following SQL statement selects all customers with a ContactName that starts with "a" and ends with "o": Command : - SELECT * FROM Customers WHERE ContactName LIKE 'a%o'; OUTPUT :-
  • 45. like - Example The following SQL statement selects all customers with a CustomerName that does NOT start with "a": Command : - SELECT * FROM Customers WHERE CustomerName NOT LIKE 'a%'; OUTPUT :-
  • 46. wildcard A wildcard character is used to substitute any other character(s) in a string. Wildcard characters are used with the SQL LIKE operator. The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards used in conjunction with the LIKE operator: % - The percent sign represents zero, one, or multiple characters _ - The underscore represents a single character
  • 47. wildcard Here are some examples showing different LIKE operators with '%' and '_' wildcards:
  • 48. wildcard - Example Command : - SELECT * FROM Customers WHERE City LIKE 'ber%'; OUTPUT :-
  • 49. IN The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions. Syntax 1 :- SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2, ...); Syntax 2 :- SELECT column_name(s) FROM table_name WHERE column_name IN (SELECT STATEMENT);
  • 50. in - Example Command : - SELECT * FROM Customers WHERE Country IN ('Germany', 'France', 'UK'); OUTPUT :-
  • 51. between The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates. The BETWEEN operator is inclusive: begin and end values are included. Syntax :- SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;
  • 52. between - Example Command : - SELECT * FROM Products WHERE Price BETWEEN 10 AND 20; OUTPUT :-
  • 53. aliases SQL aliases are used to give a table, or a column in a table, a temporary name. Aliases are often used to make column names more readable. An alias only exists for the duration of the query. Column Syntax :- SELECT column_name AS alias_name FROM table_name; Table Syntax :- SELECT column_name(s) FROM table_name AS alias_name;
  • 54. aliases for column- Example Command : - SELECT CustomerID AS ID, CustomerName AS Customer FROM Customers; OUTPUT :-
  • 55. aliases for table- Example Command : - SELECT o.OrderID, o.OrderDate, c.CustomerName FROM Customers AS c, Orders AS o WHERE c.CustomerName="Around the Horn" AND c.CustomerID=o.CustomerID; OUTPUT :-
  • 56. joins A JOIN clause is used to combine rows from two or more tables, based on a related column between them. Different Types of Joins :- i. Inner Join ii. Left Join iii. Right Join iv. Full Join v. Self Join
  • 57. joins i. Inner Join : - The INNER JOIN keyword selects records that have matching values in both tables. Syntax : - SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
  • 58. joins ii. Left Join : - The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match. Syntax : - SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;
  • 59. joins iii. Right Join : - The RIGHT JOIN keyword returns all records from the right table (table2), and the matched records from the left table (table1). The result is NULL from the left side, when there is no match. Syntax : - SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;
  • 60. joins iv. Full Join : - The FULL OUTER JOIN keyword return all records when there is a match in either left (table1) or right (table2) table records. Note: FULL OUTER JOIN can potentially return very large result-sets! Syntax : - SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;
  • 61. joins v. Self Join : - A self JOIN is a regular join, but the table is joined with itself. Syntax : - SELECT column_name(s) FROM table1 T1, table1 T2 WHERE condition;
  • 62. Union The UNION operator is used to combine the result-set of two or more SELECT statements. ● Each SELECT statement within UNION must have the same number of columns ● The columns must also have similar data types ● The columns in each SELECT statement must also be in the same order
  • 63. Union Union Syntax : - SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2; Union All Syntax : - SELECT column_name(s) FROM table1 UNION ALL SELECT column_name(s) FROM table2;
  • 65. Union - Example Command :- SELECT City FROM Customers UNION SELECT City FROM Suppliers ORDER BY City;
  • 66. Union - Example (All) Command:- SELECT City FROM Customers UNION ALL SELECT City FROM Suppliers ORDER BY City;
  • 67. Union - Example with where Command :- SELECT City, Country FROM Customers WHERE Country='Germany' UNION SELECT City, Country FROM Suppliers WHERE Country='Germany' ORDER BY City;
  • 68. Group By The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns. Syntax :- SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) ORDER BY column_name(s);
  • 69. Group by - Example Command :- SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;
  • 70. Having The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions. Syntax :- SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) HAVING condition ORDER BY column_name(s);
  • 71. Having - Example Command :- SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5;
  • 72. Exists The EXISTS operator is used to test for the existence of any record in a subquery. The EXISTS operator returns true if the subquery returns one or more records. Syntax :- SELECT column_name(s) FROM table_name WHERE EXISTS (SELECT column_name FROM table_name WHERE condition);
  • 73. Exists - Example Command :- SELECT SupplierName FROM Suppliers WHERE EXISTS (SELECT ProductName FROM Products WHERE SupplierId = Suppliers.supplierId AND Price < 20);
  • 74. Any & all The ANY and ALL operators are used with a WHERE or HAVING clause. The ANY operator returns true if any of the subquery values meet the condition. The ALL operator returns true if all of the subquery values meet the condition. ANY Syntax :- SELECT column_name(s) FROM table_name WHERE column_name operator ANY (SELECT column_name FROM table_name WHERE condition); ALL Syntax :- SELECT column_name(s) FROM table_name WHERE column_name operator ALL (SELECT column_name FROM table_name WHERE condition);
  • 75. Any & all- Example Command :- SELECT ProductName FROM Products WHERE ProductID = ANY (SELECT ProductID FROM OrderDetails WHERE Quantity = 10);
  • 76. Any & all- Example Command :- SELECT ProductName FROM Products WHERE ProductID = ALL (SELECT ProductID FROM OrderDetails WHERE Quantity = 10);
  • 77. Select into The SELECT INTO statement copies data from one table into a new table. ALL Column Syntax :- SELECT * INTO newtable [IN externaldb] FROM oldtable WHERE condition; Selected Column Syntax :- SELECT column1, column2, column3, … INTO newtable [IN externaldb] FROM oldtable WHERE condition;
  • 78. Insert into The INSERT INTO SELECT statement copies data from one table and inserts it into another table. INSERT INTO SELECT requires that data types in source and target tables match The existing records in the target table are unaffected
  • 79. Insert into Copy all columns Syntax :- INSERT INTO table2 SELECT * FROM table1 WHERE condition; Copy only some columns Syntax :- INSERT INTO table2 (column1, column2, column3, ...) SELECT column1, column2, column3, … FROM table1 WHERE condition;
  • 80. Comments Single line comments start with --.Any text between -- and the end of the line will be ignored (will not be executed). The following example uses a single-line comment as an explanation: Syntax :- --Select all: SELECT * FROM Customers; Multi-line comments start with /* and end with */.Any text between /* and */ will be ignored. Syntax :- /*Select all the columns of all the records in the Customers table:*/ SELECT * FROM Customers;