0% found this document useful (0 votes)
12 views36 pages

Master SQL: Basics to Advanced Techniques

The document provides an extensive overview of SQL, covering its syntax, commands, and advanced features. It explains the use of SQL in relational databases, including data manipulation, querying, and real-world applications. Additionally, it discusses various SQL functions, operators, and joins, along with practical examples for better understanding.

Uploaded by

CHARAN .M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views36 pages

Master SQL: Basics to Advanced Techniques

The document provides an extensive overview of SQL, covering its syntax, commands, and advanced features. It explains the use of SQL in relational databases, including data manipulation, querying, and real-world applications. Additionally, it discusses various SQL functions, operators, and joins, along with practical examples for better understanding.

Uploaded by

CHARAN .M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Mastering SQL: From

Basics to Advanced
An In-Depth Exploration of Structured Query Language
OVERVIEW

•Overview of SQL and Relational Databases


•SQL Syntax and Basic Commands
•Data Definition and Manipulation
•Advanced SQL Features
•Real-World Applications and Case Studies
•Q&A Session
 Imagine you want to analyze payment information from parking
meters across San Francisco. Maybe you want to answer questions
like:

 How many streets have parking meters?


 How much does the average parking meter make in a day?
 Do most people pay by cash or credit card?
 How many people pay to park on a given day?

 To start you will need parking meter payment data


When we think of data, a spreadsheet may be what comes to mind. This is
because a spreadsheet is “human readable” – it’s easy to see the data. But for
many data source, such as parking meter payments, this is not how data is stored
or formatted.

w s
Ro
RELATIONAL
DATABASE
 A Relational Database Management System is a type of database
management system that stores data in a structured format using
rows and columns.
-[Link]
 These programs allow users to create, update, insert, or delete data
in the system, and they provide:
 Data structure
 Multi-user access
 Privilege control
 Network access
Examples of popular RDBMS systems include MySQL, PostgreSQL, and IBM DB2.
SQL
Definition

 SQL is Structured Query Language, is a computer language for storin


g, manipulating and retrieving data stored in relational database.
HISTORY
WHY LEARN SQL?
SQL SYNTAX
AND
STRUCTURE
What is a query?

 A query is a snippet of code that requests data from a database. It


can be as simple as: “show me the data in this table” but can also be
much more complex such as: “combine these three tables together,
then only look at data from a single month, then aggregate it by a
variable, then put it in order, and only show me the first 10 rows”
Select
where

 The SQL WHERE clause is used to specify a condition while fetching


the data from single table or joining with multiple tables.

 The WHERE clause is not only used in SELECT statement, but it is


also used in UPDATE, DELETE statement, etc. We can specify
condition using >, <, = LIKE, ODER BY,GROUP BY,HAVING Etc.
 Syntax :
 SELECT column, column2, columnN FROM table_name WHERE [condition]
 Example :
 Select * from customer WHERE [cust-no]='da
SQL OPERATORS

 An operator is a reserved word or character used in SQL statement's


WHERE clause to perform operation(s).
 SELECT columnl, column2,...... columnN FROM table_name WHERE
[condition]
 They are :
 Arithmetic operators EEE
 Comparison operators
 Logical operators
SQL Arithmetic Operators :

 Operators: + ,-,*,/,%
 Examples: select * from [invoice] where [gross-amt]+[add-charges]
>1000
 select * from [invoice] where [gross-amt]-[add-charges]>0
 select * from [invoice] where [tax-amt__1]*0.5>0
SQL COMPARISON OPERATORS

 Operators: = ,l=,<, >,<>= <= I< I>


 Examples:
 Select * from customer where [cust-no]='davis’
 Select * from customer where [cust-no]!='davis’
 select * from [invoice] where [gross-amt]>100
 select * from [invoice] where [gross-amt]<0
SQL Logical Operators

 Operators :
AND , BETWEEN ,EXISTS,IN,LIKE,NOT, OR,IS NULL
 Examples:
 select * from [invoice] where [gross-amt]>0 and [add-charges]>0
 select * from [invoice] where [gross-amt]>between 0 and 10000
 select * from [sa] where [cancel-date] is null
SORTING THE DATA

 The SQL ORDER BY clause is used to sort the data in ascending or


descending order, based on one or more columns. Some database
sorts query results in ascending order by default. We can use more
than one column in the ORDER BY clause.

 Syntax:
 SELECT column(s) FROM table_name [WHERE condition] [ORDER BY
column1, .. columnN] [ASC | DESC]
 Examples:
 Select * from customer order by [cust-no] desc
 Select * from invoice order by [cust-no],[document] ASC
GROUP BY CLAUSE

 The SQL GROUP BY clause is used in collaboration with the SELECT


statement to arrange identical data into groups . Group by can be
used with WHERE ,ORDER BY clause as an optional.
 Syntax:
 SELECT column, column2 FROM table_name WHERE [ conditions ] GROUP
BY column1, column2 ORDER BY column1, column2
 SELECT SUM(column_name) FROM table_name WHERE CONDITION GROUP
BY column_name HAVING (arithmetic function condition)
 Examples:
 Select [cust-no] from [invoice] group by [cust-no]
 Select [cust-no] from invoice group by [cust-no] having count(*)>1
LIKE CLAUSE

 The SQL LIKE clause is used to compare a value to similar values using
wildcard operators.
 There are two wildcards used in conjunction with the LIKE operator:
 The percent sign (%) : The percent sign represents zero, one, or multiple
characters.
 The underscore (_) : The underscore represents a single number or character.
 Examples:
 Select * from [invoice] WHERE document like "111%'
 Select * from [invoice] WHERE document like ‘%111%’
 “ Select * from [invoice] WHERE document like ‘%111%’
 Select * from [invoice] WHERE document like '5__6%’
 Select * from [invoice] WHERE document like ‘5__000’
FUNCTIONS

 SQL has many built-in functions for performing processing on string


or numeric data.

 Following is the list of all useful built-in functions:


 Aggregate
 Date
 String
 Numeric
Aggregate Functions

 They summarize the results of a particular column of selected


numeric data. They are COUNT ,MAX,MIN,AVG and SUM. These can be
used with Group By as well.
 Examples:
 select count(*) from customer
 select max([ar-balance]) from [customer-d]
 select min([ar-balance]) from [customer-d]
 select avg([ar-balance]) from [customer-d]
 select sum([ar-balance]) from [customer-d]
 select [cust-no],sum([ar-balance]) from [customer-d] group by [cust-no]
DATE FUNCTIONS
 They are quite a few in-built date functions like GETDATE, DAY, MONTH, YEAR DATENAME, DATEADD, DATEDIFF etc.
 GETDATE - Gives current date time stamp
 DAY - Returns an integer representing the day (day of the month) of the specified date MONTH Returns an integer that represents
the month of the specified date
 YEAR - Returns an integer that represents the year of the specified date
 DATENAME - Syntax DATENAME ( datepart , date | where the datepart is DAY, MONTH YEAR, WEEK, WEEKDAY Etc.
 DATEADD - Returns a specified date with the integer added to a specified datepart of that date. Syntax: DATEADD (datepart,
number , date |
 DATEDIFF - Returns the count of the specified datepart between the specified start date and end date. Syntax: DATEDIFF
(datepart,startdate,enddate)
 Examples:
 select GETDATE ()
 select * from invoice where DAY([invoice-date])=1
 select * from invoice where DAY([invoice-date])=1 and MONTH|[invoice-date])=10
 select * from invoice where DAY([invoice-date])=1 and MONTH ([invoice date])=10 and YEAR ([invoice-date])=2013
 select * from invoice where DATENAME( week ,[invoice-date])=4 and MONTH ([invoice-date])=1
 select * from invoice where DATENAME (weekday, [invoice-date])='Sunday’ and MONTH ([invoice-date])=1
 select * from invoice where DATEADD(YEAR, 1, ([invoice-date])=2014
 select * from invoice where DATEDIFF(day,[invoice-date] ,[due-date])> 10
STRING FUNCTIONS
 SQL string functions are used primarily for string manipulation. The following are the important string functions:
 CHARINDEX: Returns the starting position of a character string.
 LEFT :Returns the leftmost number of characters as specified
 RIGHT :Returns the specified rightmost number of characters
 SUBSTRING: Returns the substring as specified
 LTRIM/RTRIM: Removes leading and trailing spaces
 LOWER: Returns the argument in lowercase
 UPPER: Converts to uppercase
 LEN: Returns the length of a string in bytes
 REVERSE: Returns reverse a string.
 REPLACE: Replaces occurrences of a specified string
 Examples:
 Select * from customer where CHARINDEX('Davis', Name)>0
 Select * from customer where SUBSTRING( Name , 1,4)='david’
 Select * from customer where LEN( City )=15
 Select UPPER(name) from customer
 Select LOWER(name) from customer
 Select LTRIM(name) from customer
 Select LEFT(name,5),* from customer asc)
 Select RIGHT(name, 10),* from customer
 Select REVERSE(name) from customer
NUMERIC FUNCTIONS

 SQL numeric functions are used for numeric manipulation or mathematical


calculation.
 CEILING: Returns the smallest integer value that is not less than passed numeric
expression.
 FLOOR: Returns the largest integer value that is not greater than passed numeric
expression.
 ROUND: Returns numeric expression rounded to an integer. Can be used to round
an expression to a number of decimal points

 Examples:
 select CEILING([gross-amt]) ‘Gross Amt’, * from [sa-inv]
 select FLOOR([gross-amt]) ‘Gross Amt’, * from [sa-iNV]
 select ROUND([gross-amt],3) ‘Gross Amt’, * from [sa-inv]
JOINS

 The SQL Join clause is used to combine records from multiple tables in
a database. We can join with join two, three or n number tables.
 There are different types of joins available in SOL:
 INNER JOIN : Returns rows when there is a match in both tables.
 LEFT JOIN: Returns all rows from the left table, even if there are no
matches in the right table.
 RIGHT JOIN : Returns all rows from the right table, even if there are no
matches in the left table.
INNER JOIN

 The most frequently used and important of the joins is the INNER JOIN. They are also referred to as an
[Link] INNER JOIN creates a new result table by combining column values of two tables.
 Syntax:
 SELECT table column, table2.column2... FROM table1
INNER JOIN table2 ON table 1.common_filed = table2.common_field
 Example:
 SELECT customer [cust-no], [customer-d] [ar-balance] from customer
INNER JOIN [customer-d] on customer. [cust-no]=[customer-d] [cust-no]
WHERE active=a’ and [customer-d] [ar-balance]>1000
 SELECT c [custno],d [ar-balance] from customer c with(nolock)
INNER JOIN [customer-d] d with(nolock) on c [cust-no]=d [cust-no]
WHERE [Link]=‘a’ and d.[ar-balance]>1000
 SELECT [job]. [cust-no], [job]. [job-no],[jc-inv]. [document] [ar-term]. [Term-desc] ,
[jc-inv].[gross-amt]+[jc-inv].[adc-charges] ‘Inv Amt’ from [job] with(nolock)
INNER JOIN [jc-inv] with(nolock) on [job].[Job-no]=[jc-inv].[Job-no]
INNER JOIN [ar-term] with(nolock) on [job] [term-code]~[ar-term] [term-cod WHERE [job] [statuscode]~"open’
SQL –Sub Queries

 A Subquery or Nested query is a query within another SQL query and with WHERE clause. A subquery is used to
return data that ll be used’in the main query as a condition to further filter the data to be retrieved.
 Sub queries are used with SELECT, INSERT, UPDATE, and DELETE statements with the operators like =, <, >, >=,
<= [N, BETWEEN etc.
 Here are a few rules that subqueries must follow:
 Subqueries must be enclosed within parentheses.
 A subquery can have only one column in the SELECT clause, unless multiple columns are in the main
 query for the subquery to compare its selected columns.
 An ORDER BY cannot be used in a subquery, although the main query can use an ORDER BY. The GROUP BY can be
used to perform the same function as the ORDER in a subquery.
 Subqueries that return more than one row can only be used with multiple value operators, such as the IN operator.
 Syntax:
 SELECT column_name [, column_name ] FROM table | WHERE column_name OPERATOR
(SELECT column_name FROM table2 [WHERE])
 Example:
 select * from “item” where “item-no” in (select "whs-code" from warehouse) )
 select * from “item” where ‘item-no" not in (select “whs-code” from warehouse)
DML – Data Manipulation Language

 DML is used to modify, delete, insert data in database. DML Commands are INSERT,
UPDATE, and DELETE.
 UPDATE :
 The SOL UPDATE Query is used to modify the existing records in a table.
 You have to use WHERE clause with UPDATE query to update selected rows otherwise all
the rows will be updated.
 Syntax:
 UPDATE table_name SET column1 = value1, column2 = value2....,
columnN = valueN WHERE [condition]

 Example:
 update [sctype] set [price]=100 where [sctype]="Test’ )
 update [sctype] set [price]=100 where [sctype] in (‘Test’, ‘Test1’ ,‘Test2’)

You might also like