100% found this document useful (1 vote)
102 views22 pages

SQL Queries: Week 7

This document discusses various SQL commands categorized into DDL, DQL, DML, and DCL. It provides examples of common SQL queries using SELECT, WHERE, ORDER BY, GROUP BY, JOIN, and other clauses. Examples are shown to retrieve, filter, sort, group, and manipulate data from sample tables. The document also provides questions to test understanding of the different SQL queries and concepts covered.
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
100% found this document useful (1 vote)
102 views22 pages

SQL Queries: Week 7

This document discusses various SQL commands categorized into DDL, DQL, DML, and DCL. It provides examples of common SQL queries using SELECT, WHERE, ORDER BY, GROUP BY, JOIN, and other clauses. Examples are shown to retrieve, filter, sort, group, and manipulate data from sample tables. The document also provides questions to test understanding of the different SQL queries and concepts covered.
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
You are on page 1/ 22

SQL Queries

Week 7

Muhammad Qasim
SQL Commands
These SQL commands are mainly categorized into four categories as:
 DDL – Data Definition Language

◦ CREATE – is used to create the database or its objects (like table, index,
function, views, store procedure and triggers).
◦ DROP – is used to delete objects from the database.
◦ ALTER-is used to alter the structure of the database.
 DQL – Data Query Language
◦ SELECT – is used to retrieve data from the a database.
 DML – Data Manipulation Language
◦ INSERT – is used to insert data into a table.
◦ UPDATE – is used to update existing data within a table.
◦ DELETE – is used to delete records from a database table.
 DCL – Data Control Language
◦ GRANT-gives user’s access privileges to database.
◦ REVOKE-withdraw user’s access privileges given by using the GRANT command.
Select
Selecting individual columns of data from table:
Syntax:
SELECT column1, column2, ...
FROM table_name;

Example:
SELECT FirstName, LastName, City 
FROM Customers;

Selecting all columns of data from table:


Syntax:
SELECT * 
FROM table_name;

Example:
SELECT *
FROM Customers;
Select Distinct
Syntax:
SELECT DISTINCT column1, column2, ...
FROM table_name;

Example:
SELECT DISTINCT Country FROM Customers;
Where
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Example:
SELECT FirstName, LastName, City 
FROM Customer
WHERE State = 'NY‘ or state = ‘TX’;

Example:
SELECT FirstName, LastName, City 
FROM Customer
WHERE State = 'NY’ or ‘TX’;

Example:
SELECT FirstName, LastName, City 
FROM Customers
WHERE State = 'NY’ AND Creditlimit = 2000;
And, Or, Not
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
Example:
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';

SELECT * FROM Customers
WHERE City='Berlin' OR City='München';

SELECT * FROM Customers
WHERE Country='Germany' OR Country='Spain';
Order By
Syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

Example:
SELECT FirstName, LastName, City 
FROM Customer
Order By FirstName DESC;
Order By
Syntax:
SELECT column_names
FROM table_name
WHERE column_name IS NULL;

Example:
SELECT FirstName, LastName, City 
FROM Customer
WHERE PostalCode is NULL;
Select Top
Syntax:
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;

Example:
SELECT TOP 3 * FROM Customers;
Min and Max
Min():
Syntax:
SELECT MIN(column_name)
FROM table_name
WHERE condition;

Example:
SELECT MIN(CreditLimit)
FROM Customer;

Max():
Syntax:
SELECT MAX(column_name)
FROM table_name
WHERE condition;

Example:
SELECT MAX(CreditLimit)
FROM Customer;
Count, Avg, Sum
Count():
Syntax:
SELECT Count(column_name)
FROM table_name
WHERE condition;

Avg():
Syntax:
SELECT Avg(column_name)
FROM table_name
WHERE condition;

Sum():
Syntax:
SELECT Sum(column_name)
FROM table_name
WHERE condition;
Like
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

Syntax:
SELECT *
from Employee As e
where e.FirstName like "A*"
Wildcards
 SELECT * FROM Customers
WHERE City LIKE 'ber%';

 SELECT * FROM Customers
WHERE City LIKE '_ondon';

 The following SQL statement selects all customers with a City starting with "b", "s", or "p":
 SELECT * FROM Customers
WHERE City LIKE '[bsp]%';

 The following SQL statement selects all customers with a City starting with "a", "b", or "c":
 SELECT * FROM Customers
WHERE City LIKE '[a-c]%';
In
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

Example:
SELECT FirstName, LastName, City 
FROM Customer
WHERE State in (‘NY’,’TX’);
Between
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Example:
SELECT * 
FROM Customer
WHERE CreditLimit BETWEEN 1000 AND 1500;

SELECT *
FROM Customer As c
where c.customersince BETWEEN #01-Jan-2010# and #01-Jan-2015#;
Aliases
Syntax:
SELECT column_name AS alias_name
FROM table_name;

Example:
SELECT FirstName As n, LastName As l, City As c
FROM Customer;
Union, Union all
UNION: (Doesn’t allow duplicate)

Syntax:
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

UNION All: (Allows duplicates)

Syntax:
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
Groupby
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GRUP BY column_name(s)

Example:

SELECT state, count(customerID) As Counts


from Customer
group by State;
Having
Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

Example:

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
Exists
Syntax:
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);

Example:

SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID =
Suppliers.supplierID AND Price < 20);
Sample Table
Questions
 Select all the customers who are not from united states?
 Select all the customers who’s country are not given?
 Select all the customers who’s don’t have any discount?
 Provide name, address, city and country who’s are American but don’t have county
mentioned?
 Provide Name, City, State, Phone Number of those customers who’s credit limit is above
$1000?
 Provide Name, Phone Number of those customers who’s credit limit is null?

You might also like