You are on page 1of 19

Unit- V

SQL Basics
R.Sheeba M.Sc.,M.Phil.,
Assistant professor,
Department of computer science
Madurai sivakasi nadars pioneer Meenakshi women’s
College, poovanthi
Structured Query Language (SQL)

 SQL is a standard data access language used to interact with relational databases.

 Microsoft SQL server – To use SQL to create fairly sophisticated SQL scripts for stored
procedures and triggers.
SQL Statements

Types of SQL statemets

 SELECT: Use a Select statement to retrieve records

 UPDATE: Use an update statement to modify records

 INSERT: Use an Insert statement to add a new record

 DELETE: Use a Delete statement to delete existing records


The SQL Select Statement
 The SELECT statement is used to select data from a database.

Syntax
The basic syntax of the ORDER BY clause is as follows −

SELECT column-list FROM table_name [WHERE condition] [ORDER


BY column1, column2, .. columnN] [ASC | DESC];

The SQL ORDER BY clause is used to sort the data in ascending or descending order,
based on one or more columns. Some databases sort the query results in an ascending
order by default.
Example: Select Statement

Here, the SQL command selects the first_name and last_name of all Customers.

SELECT first_name, last_name FROM Customers;


SQL SELECT ALL (*)
To select all columns from a database table, we use the asterisk (*) character. For example,

SELECT * FROM Customers;

Here, the SQL command selects all columns of the Customers table.
SQL SELECT WHERE Clause
 A SELECT statement can have an optional WHERE clause.
 The WHERE clause allows us to fetch records from a database table
that matches specified condition(s).
For example,

SELECT age, country FROM Customers WHERE country = 'USA';

Here, the SQL command fetches age and country fields of all customers
whose country is USA.
The SQL Update
In SQL, the UPDATE statement is used to edit the existing record in
a database table.

Syntax:

UPDATE [table] SET [Update_expression] WHERE [search condition]

Example,

UPDATE Customers SET first_name = 'Johnny' WHERE customer_id = 1;

Here, the SQL command changes the value of the first_name column will be Johnny if
customer_id is equal to 1.
Update Multiple Values in a Row

 We can also update multiple values in a row at once. For example,

UPDATE Customers SET first_name = 'Johnny', last_name = 'Depp' WHERE customer_id = 1;

Here, the SQL command changes the value of the first_name column to Johnny and
last_name to Depp if customer_id is equal to 1.
Update Multiple Records
The UPDATE statement can update multiple records at once.
For example,

UPDATE Customers SET country = 'NP' WHERE age = 22;

Here, the SQL command changes the value of the country column to NP if age is
22. If there are more than one rows with age equals to 22, all the matching rows
will be edited.
The SQL Insert Statement
In SQL, the INSERT INTO statement is used to insert new record(s) in
a database table.
Syntax:
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN) VALUES (value1, value2, value3,...valueN);

For example,

INSERT INTO Customers(customer_id, first_name, last_name, age, country)


VALUES (5, 'Harry', 'Potter', 31, 'USA');
Here, the SQL command inserts a new record in the Customers table with the given values.
The SQL Delete Statement
In SQL, we use the DELETE statement to delete row(s) from a database table.
For example,

DELETE FROM Customers WHERE customer_id = 5;

Here, the SQL command will delete a row from the Customers table where customer_id is
5.
Delete all Rows in a Table
The WHERE clause determines which rows to delete. However, we can
delete all rows at once if we omit the WHERE clause.
For example,

DELETE FROM Customers;

Here, the SQL command deletes all rows from a table.

You might also like