SlideShare a Scribd company logo
SQL BASIC QUERIES
WHAT IS SQL
SQL is a standard language for accessing and manipulating databases.
What is SQL?
•SQL stands for Structured Query Language
•SQL lets you access and manipulate databases
•SQL is an ANSI (American National Standards Institute) standard
What Can SQL do?
•SQL can execute queries against a database
•SQL can retrieve data from a database
•SQL can insert, update and create records in a database
•SQL can delete records from 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
•SQL can create new databases
REMEMBER
• SQL is NOT case sensitive: select is the same as
SELECT, however it is better to follow the same style.
• Some database systems require a semicolon at the end
of each SQL statement.
• Semicolon is the standard way to separate each SQL
statement in database systems that allow more than
one SQL statement to be executed in the same call to
the server.
SQL QUERIES
• Few important queries are:
• SELECT - extracts data from a database
• UPDATE - updates data in a database
• DELETE - deletes data from a database
• INSERT INTO - inserts new data into a database
• CREATE DATABASE - creates a new database
• ALTER DATABASE - modifies a database
• CREATE TABLE - creates a new table
• ALTER TABLE - modifies a table
• DROP TABLE - deletes a table
SQL SELECT
• The SELECT statement is used to select data from a
database.The result is stored in a result table, called the
result-set.
• Syntax:
• SELECT column_name,column_name FROM table_name;
and
• SELECT * FROM table_name;
SQL DISTINCT
• The SELECT DISTINCT statement is used to return only distinct
(different) values
• In a table, a column may contain many duplicate values; and
sometimes you only want to list the different (distinct) values. The
DISTINCT keyword can be used to return only distinct (different)
values.
• Syntax
• SELECT DISTINCT column_name,column_name
FROM table_name;
SQL WHERE
• The WHERE clause is used to filter records.
• The WHERE clause is used to extract only those
records that fulfill a specified criterion.
• Syntax
• SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
SQL AND / OR
• The AND & OR operators are used to filter records
based on more than one condition.
• 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.
• Syntax:
• SELECT * FROM Customers
WHERE Country='Germany'
AND City='Berlin';
SQL ORDER BY
• The ORDER BY keyword is used to sort the result-set.
• 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.
• Syntax
• SELECT column_name, column_name
FROM table_name
ORDER
BY column_name ASC|DESC, column_name ASC|DES
C;
SQL UPDATE
• The UPDATE statement is used to update existing records in a table.
• Syntax
• UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
• The WHERE clause specifies which record or records that should be
updated. If you omit the WHERE clause, all records will be updated!
SQL DELETE
• The DELETE statement is used to delete records in a table.
• The SQL DELETE Statement
• The DELETE statement is used to delete rows in a table.
• Syntax
• DELETE FROM table_name
WHERE some_column=some_value;
• The WHERE clause specifies which record or records that should be
deleted. If you omit the WHERE clause, all records will be deleted!
SQL SELECT TOP
• The SELECT TOP clause is used to specify the number of
records to return.
• The SELECT TOP clause can be very 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.
• Syntax
• SELECT TOP number|percent column_name(s)
FROM table_name;
SQL LIKE
• The LIKE operator is used in a WHERE clause to search for a specified
pattern in a column.
• The LIKE operator is used to search for a specified pattern in a column.
• Syntax
• SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
SQL IN
• The IN operator allows you to specify multiple values in
a WHERE clause.
• Syntax
• SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);
SQL BETWEEN
• The BETWEEN operator is used to select values within a range.
• The BETWEEN operator selects values within a range. The values can be numbers, text,
or dates.
• Syntax
• SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
SQL ALIAS
• 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.
• Alias Syntax for Columns
• SELECT column_name AS alias_name
FROM table_name;
• Alias Syntax for Tables
• SELECT column_name(s)
FROM table_name AS alias_name;
SELECT INTO
• With SQL, you can copy information from one table into another.
• The SELECT INTO statement copies data from one table and inserts it into a new table.
• Syntax
• We can copy all columns into the new table:
• SELECT *
INTO newtable
FROM table1;
• Or we can copy only the columns we want into the new table:
• SELECT column_name(s)
INTO newtable FROM table1;
• The new table will be created with the column-names and types as defined in the SELECT
statement. You can apply new names using the AS clause.
•
SQL INSERT
• The INSERT INTO statement is used to insert new records in a table.
• Syntax
• It is possible to write the INSERT INTO statement in two forms.
• The first form does not specify the column names where the data will be
inserted, only their values:
• INSERT INTO table_name
VALUES (value1,value2,value3,...);
• The second form specifies both the column names and the
values to be inserted:
• INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
•
INSERT INTO
• The INSERT INTO SELECT statement copies data from one table and
inserts it into an existing table.
• The INSERT INTO SELECT statement selects data from one table
and inserts it into an existing table. Any existing rows in the target
table are unaffected.
• Syntax
• We can copy all columns from one table to another, existing table:
• INSERT INTO table2
SELECT * FROM table1;
• Or we can copy only the columns we want to into another, existing
table:
• INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;
CREATE DATABASE
• The CREATE DATABASE statement is used to create a
database.
• Syntax
• CREATE DATABASE dbname;
• SQL CREATE DATABASE Example
• The following SQL statement creates a database called
"my_db":
• CREATE DATABASE my_db;
CREATE TABLE
• The CREATE TABLE 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),
column_name3 data_type(size),
....
);
• The column_name parameters specify the names of the columns of the
table.
• The data_type parameter specifies what type of data the column can hold
(e.g. varchar, integer, decimal, date, etc.).
• The size parameter specifies the maximum length of the column of the table.

More Related Content

PPTX
SQL
Jerin John
 
PPTX
SQL
Jerin John
 
PPTX
SQl data base management and design
franckelsania20
 
PPTX
Sql slid
pacatarpit
 
PPTX
SQL.pptx for the begineers and good know
PavithSingh
 
PPTX
shs tvl ict_Programming Introduction to SQl.pptx
Joseph Camarote
 
PPTX
Avinash database
avibmas
 
PPTX
Sql
Aman Lalpuria
 
SQl data base management and design
franckelsania20
 
Sql slid
pacatarpit
 
SQL.pptx for the begineers and good know
PavithSingh
 
shs tvl ict_Programming Introduction to SQl.pptx
Joseph Camarote
 
Avinash database
avibmas
 

Similar to 2..basic queries.pptx (20)

PPTX
SQL
Shyam Khant
 
PPTX
Sql basic things
Nishil Jain
 
PPTX
SQL Query
Imam340267
 
PPT
chapter 8 SQL.ppt
YitbarekMurche
 
PPT
SQL. It education ppt for reference sql process coding
aditipandey498628
 
PDF
Structure query language, database course
yunussufyan2024
 
DOCX
SQL report
Ahmad Zahid
 
PDF
full detailled SQL notesquestion bank (1).pdf
yvpachorib23
 
PDF
Chapter – 6 SQL Lab Tutorial.pdf
TamiratDejene1
 
PDF
Database Management System 1
Swapnali Pawar
 
PDF
DBMS.pdf
Rishab Saini
 
PPT
Sql Tutorials
Priyabrat Kar
 
PPT
CE 279 - WRITING SQL QUERIES umat edition.ppt
minusahsaaka
 
PPTX
slides about : Introduction_to_SQL.pptx
DrMarwaElsherif
 
PPTX
Lesson-02 (1).pptx
ssuserc24e05
 
DOC
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
Newyorksys.com
 
PPT
MY SQL
sundar
 
PPTX
SQL Tutorial for Beginners
Abdelhay Shafi
 
PPTX
4 SQL DML.pptx ASHEN WANNIARACHCHI USESS
nimsarabuwaa2002
 
PPTX
Lab1 select statement
Balqees Al.Mubarak
 
Sql basic things
Nishil Jain
 
SQL Query
Imam340267
 
chapter 8 SQL.ppt
YitbarekMurche
 
SQL. It education ppt for reference sql process coding
aditipandey498628
 
Structure query language, database course
yunussufyan2024
 
SQL report
Ahmad Zahid
 
full detailled SQL notesquestion bank (1).pdf
yvpachorib23
 
Chapter – 6 SQL Lab Tutorial.pdf
TamiratDejene1
 
Database Management System 1
Swapnali Pawar
 
DBMS.pdf
Rishab Saini
 
Sql Tutorials
Priyabrat Kar
 
CE 279 - WRITING SQL QUERIES umat edition.ppt
minusahsaaka
 
slides about : Introduction_to_SQL.pptx
DrMarwaElsherif
 
Lesson-02 (1).pptx
ssuserc24e05
 
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
Newyorksys.com
 
MY SQL
sundar
 
SQL Tutorial for Beginners
Abdelhay Shafi
 
4 SQL DML.pptx ASHEN WANNIARACHCHI USESS
nimsarabuwaa2002
 
Lab1 select statement
Balqees Al.Mubarak
 

Recently uploaded (20)

PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Basics and rules of probability with real-life uses
ravatkaran694
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 

2..basic queries.pptx

  • 2. WHAT IS SQL SQL is a standard language for accessing and manipulating databases. What is SQL? •SQL stands for Structured Query Language •SQL lets you access and manipulate databases •SQL is an ANSI (American National Standards Institute) standard What Can SQL do? •SQL can execute queries against a database •SQL can retrieve data from a database •SQL can insert, update and create records in a database •SQL can delete records from 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 •SQL can create new databases
  • 3. REMEMBER • SQL is NOT case sensitive: select is the same as SELECT, however it is better to follow the same style. • Some database systems require a semicolon at the end of each SQL statement. • Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.
  • 4. SQL QUERIES • Few important queries are: • SELECT - extracts data from a database • UPDATE - updates data in a database • DELETE - deletes data from a database • INSERT INTO - inserts new data into a database • CREATE DATABASE - creates a new database • ALTER DATABASE - modifies a database • CREATE TABLE - creates a new table • ALTER TABLE - modifies a table • DROP TABLE - deletes a table
  • 5. SQL SELECT • The SELECT statement is used to select data from a database.The result is stored in a result table, called the result-set. • Syntax: • SELECT column_name,column_name FROM table_name; and • SELECT * FROM table_name;
  • 6. SQL DISTINCT • The SELECT DISTINCT statement is used to return only distinct (different) values • In a table, a column may contain many duplicate values; and sometimes you only want to list the different (distinct) values. The DISTINCT keyword can be used to return only distinct (different) values. • Syntax • SELECT DISTINCT column_name,column_name FROM table_name;
  • 7. SQL WHERE • The WHERE clause is used to filter records. • The WHERE clause is used to extract only those records that fulfill a specified criterion. • Syntax • SELECT column_name,column_name FROM table_name WHERE column_name operator value;
  • 8. SQL AND / OR • The AND & OR operators are used to filter records based on more than one condition. • 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. • Syntax: • SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin';
  • 9. SQL ORDER BY • The ORDER BY keyword is used to sort the result-set. • 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. • Syntax • SELECT column_name, column_name FROM table_name ORDER BY column_name ASC|DESC, column_name ASC|DES C;
  • 10. SQL UPDATE • The UPDATE statement is used to update existing records in a table. • Syntax • UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value; • The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!
  • 11. SQL DELETE • The DELETE statement is used to delete records in a table. • The SQL DELETE Statement • The DELETE statement is used to delete rows in a table. • Syntax • DELETE FROM table_name WHERE some_column=some_value; • The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!
  • 12. SQL SELECT TOP • The SELECT TOP clause is used to specify the number of records to return. • The SELECT TOP clause can be very 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. • Syntax • SELECT TOP number|percent column_name(s) FROM table_name;
  • 13. SQL LIKE • The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. • The LIKE operator is used to search for a specified pattern in a column. • Syntax • SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern;
  • 14. SQL IN • The IN operator allows you to specify multiple values in a WHERE clause. • Syntax • SELECT column_name(s) FROM table_name WHERE column_name IN (value1,value2,...);
  • 15. SQL BETWEEN • The BETWEEN operator is used to select values within a range. • The BETWEEN operator selects values within a range. The values can be numbers, text, or dates. • Syntax • SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;
  • 16. SQL ALIAS • 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. • Alias Syntax for Columns • SELECT column_name AS alias_name FROM table_name; • Alias Syntax for Tables • SELECT column_name(s) FROM table_name AS alias_name;
  • 17. SELECT INTO • With SQL, you can copy information from one table into another. • The SELECT INTO statement copies data from one table and inserts it into a new table. • Syntax • We can copy all columns into the new table: • SELECT * INTO newtable FROM table1; • Or we can copy only the columns we want into the new table: • SELECT column_name(s) INTO newtable FROM table1; • The new table will be created with the column-names and types as defined in the SELECT statement. You can apply new names using the AS clause. •
  • 18. SQL INSERT • The INSERT INTO statement is used to insert new records in a table. • Syntax • It is possible to write the INSERT INTO statement in two forms. • The first form does not specify the column names where the data will be inserted, only their values: • INSERT INTO table_name VALUES (value1,value2,value3,...); • The second form specifies both the column names and the values to be inserted: • INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...); •
  • 19. INSERT INTO • The INSERT INTO SELECT statement copies data from one table and inserts it into an existing table. • The INSERT INTO SELECT statement selects data from one table and inserts it into an existing table. Any existing rows in the target table are unaffected. • Syntax • We can copy all columns from one table to another, existing table: • INSERT INTO table2 SELECT * FROM table1; • Or we can copy only the columns we want to into another, existing table: • INSERT INTO table2 (column_name(s)) SELECT column_name(s) FROM table1;
  • 20. CREATE DATABASE • The CREATE DATABASE statement is used to create a database. • Syntax • CREATE DATABASE dbname; • SQL CREATE DATABASE Example • The following SQL statement creates a database called "my_db": • CREATE DATABASE my_db;
  • 21. CREATE TABLE • The CREATE TABLE 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), column_name3 data_type(size), .... ); • The column_name parameters specify the names of the columns of the table. • The data_type parameter specifies what type of data the column can hold (e.g. varchar, integer, decimal, date, etc.). • The size parameter specifies the maximum length of the column of the table.