You are on page 1of 19

SQL SERVER

 What is SQL?
• SQL stands for Structured Query Language
• SQL lets you access and manipulate databases

 What Can SQL 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
 RDBMS
• RDBMS stands for Relational Database Management System.

• RDBMS is the basis for SQL, and for all modern database systems.

• 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.
• A database most often contains one or more tables. Each table is identified
by a name. Tables contain records (rows) with data.

• Every table is broken up into smaller entities called fields.

• A field is a column in a table that is designed to maintain specific information


about every record in the table.

• A record, also called a row, is each individual entry that exists in a table.
SQL Statements
• Most of the actions you need to perform on a database are done
with SQL statements.
Example

SELECT *FROMCustomers;

• SQL keywords are NOT case sensitive: select is the same as SELECT
Some of The Most Important SQL Commands

• 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
The SQL WHERE Clause
• The WHERE clause is used to filter records.
• It is used to extract only those records that fulfill a specified condition.

The SQL AND, OR and NOT Operators


• 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 SQL INSERT INTO Statement

• The INSERT INTO statement is used to insert new records in a table.

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);

The SQL IN Operator

• The IN operator allows you to specify multiple values in a WHERE clause.

• The IN operator is a shorthand for multiple OR conditions.


The SQL UPDATE Statement

• The UPDATE statement is used to modify the existing records in a table.

UPDATE table_name SET column1 = value1, column2 = value2, ...


WHERE condition;

The SQL DELETE Statement

• The DELETE statement is used to delete existing records in a table.

DELETE FROM table_name WHERE condition;


The SQL SELECT TOP 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 performance.

SELECT TOP number|percent column_name(s)


FROM table_name
WHERE condition;
The SQL MIN() and MAX() Functions
• The MIN() function returns the smallest value of the selected column.

• The MAX() function returns the largest value of the selected column.

The SQL COUNT(), AVG() and SUM() Functions

• The COUNT() function returns the number of rows that matches a


specified criterion.

• The AVG() function returns the average value of a numeric column.

• The SUM() function returns the total sum of a numeric column.


The SQL LIKE Operator
• The LIKE operator is used in a WHERE clause to search for a specified
pattern in a column.

SELECT column1, column2, ...


FROM table_name
WHERE columnN LIKE pattern;
The SQL BETWEEN Operator

• 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.

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
SQL JOIN

• A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.

Different Types of SQL JOINs


• (INNER) JOIN: Returns records that have matching values in both tables
• LEFT (OUTER) JOIN: Returns all records from the left table, and the matched
records from the right table
• RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the left table
• FULL (OUTER) JOIN: Returns all records when there is a match in either left or right
table
The SQL GROUP BY Statement

• The GROUP BY statement groups rows that have the same values into
summary rows

• 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.
What is a Stored Procedure?

• A stored procedure is a prepared SQL code that you can save, so the code can be
reused over and over again.
• So if you have an SQL query that you write over and over again, save it as a stored
procedure, and then just call it to execute it.
• You can also pass parameters to a stored procedure, so that the stored procedure
can act based on the parameter value(s) that is passed.

• CREATE PROCEDURE procedure_name


• AS
• sql_statement
• GO;

• Execute a Stored Procedure


EXEC procedure_name;
Trigger
• A SQL trigger is a database object which fires when an event occurs in a
database.

• We can execute a SQL query that will "do something" in a database when a
change occurs on a database table such as a record is inserted or updated or
deleted.
Functions

• Like functions in programming languages, SQL Server user-defined functions are


accept parameters, perform an action, and return the result of that action as a
value.

• 1. Scalar - Valued Function: Return a single value

• 2. Table - Valued Function: Return no.of rows & columns


THANK YOU

You might also like