0% found this document useful (0 votes)
46 views

SQL For Mis305

The document discusses various SQL statements and clauses used to query and manipulate data in a database. It covers the SELECT statement for retrieving data, the WHERE clause for filtering records, operators like LIKE, AND and OR, aggregate functions like COUNT and AVG, the GROUP BY clause for grouping results, and HAVING clause for filtering group results. Examples are provided for each concept to demonstrate their usage in SQL queries.

Uploaded by

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

SQL For Mis305

The document discusses various SQL statements and clauses used to query and manipulate data in a database. It covers the SELECT statement for retrieving data, the WHERE clause for filtering records, operators like LIKE, AND and OR, aggregate functions like COUNT and AVG, the GROUP BY clause for grouping results, and HAVING clause for filtering group results. Examples are provided for each concept to demonstrate their usage in SQL queries.

Uploaded by

Rashedul Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SQL for MIS305(5&6)

The SQL SELECT Statement:


The SELECT statement is used to select data from a database.
Syntax:
SELECT *
FROM table_name;

SELECT column1, column2, ...


FROM table_name;
Example:
➢ SELECT *
FROM Student;
➢ SELECT StdFirstName, StdLastName
FROM Student;

The SQL WHERE Clause:


The WHERE clause is used to filter records. The WHERE clause is used to extract only those records
that fulfill a specified condition.

Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
➢ SELECT StdFirstName, StdLastName, StdCity
FROM Student
WHERE StdCity=” SEATTLE”

➢ SELECT StdFirstName, StdLastName, StdCity, StdGPA


FROM Student
WHERE StdGPA=3
Conditions for Number: [Use all condition for Practice)

= Equal 3

<> Not Equal 3


> Greater Than 3

< Less Than 3


>= Greater than or 3
Equal
<= Less than or Equal 3

BETWEEN Range 3 AND 4

IN Specific Number (3,3.2,3.5,3.7)

The SQL LIKE Operator:


The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator in MS Access
* The percent sign represents zero, one, or multiple characters. For example: bl* finds bl, black,
blue, and blob
? The underscore represents a single character. For example: h?t finds hot, hat, and hit

Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

Example:
➢ SELECT StdFirstName, StdLastName, StdCity
FROM Student
WHERE StdCity Like ” SEATTLE”

➢ SELECT StdFirstName, StdLastName, StdCity


FROM Student
WHERE StdCity Not Like ” SEATTLE”
➢ SELECT StdFirstName, StdLastName, StdCity
FROM Student
WHERE StdCity Like ” S*”

➢ SELECT StdFirstName, StdLastName, StdCity


FROM Student
WHERE StdCity Like ” SE*”

➢ SELECT StdFirstName, StdLastName, StdCity


FROM Student
WHERE StdCity Like ” *E”

➢ SELECT StdFirstName, StdLastName, StdCity


FROM Student
WHERE StdCity Like ” *LE”

➢ SELECT StdFirstName, StdLastName, StdCity


FROM Student
WHERE StdCity Like ” SE?????”

➢ SELECT StdFirstName, StdLastName, StdCity


FROM Student
WHERE StdCity Like ” ?????LE”

The SQL ORDER BY Keyword:


The ORDER BY keyword is used to sort the result-set in ascending or descending order.The
ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword.

ORDER BY Syntax:

SELECT column1, column2, ...


FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

Example:
SELECT StdFirstName, StdLastName
FROM Student
ORDER BY StdFirstName

SELECT StdFirstName, StdLastName


FROM Student
ORDER BY StdFirstName DESC

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 AND operator displays a record if all the conditions separated by AND are
TRUE.
➢ The OR operator displays a record if any of the conditions separated by OR is
TRUE.
▪ The NOT operator displays a record if the condition(s) is NOT TRUE.

AND Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

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

NOT Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

Example:

➢ SELECT *
FROM Student
WHERE StdCity ="SEATTLE" AND StdMajor= "IS";

➢ SELECT *
FROM Student
WHERE StdCity ="SEATTLE" OR StdMajor= "IS";

➢ SELECT *
FROM Student
WHERE NOT StdCity ="SEATTLE"

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.

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

Example:
➢ SELECT *
FROM Student
WHERE StdCity IN(“SEATTLE”,” BOTHELL”)

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.

Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Example:
➢ SELECT StdFirstName, StdLastName, StdCity, StdGPA
FROM Student
WHERE StdGPA BETWEEN(3,4)

SQL Aggregate Function: An aggregate function performs a calculation one or more values
and returns a single value.

Example of Aggregate Functions:

➢ AVG – calculates the average of a set of values.


➢ COUNT – counts rows in a specified table or view.
➢ MIN – gets the minimum value in a set of values.
➢ MAX – gets the maximum value in a set of values.
➢ SUM – calculates the sum of values.

Syntax:
SELECT Aggregate Function(column_name)
FROM table_name
WHERE condition;
Example:
➢ SELECT COUNT(StdSSN)
FROM Student;

➢ SELECT MIN(StdGPA)
FROM Student;

➢ SELECT MAX(StdGPA)
FROM Student;

➢ SELECT AVG(StdGPA)
FROM Student;

➢ SELECT SUM(FacSalary)
FROM Faculty

GROUP BY: The GROUP BY statement groups rows that have the same values into summary rows, like
"find the number of customers in each country". 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.

Syntax: SELECT column_name(s)


FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

Example:

➢ SELECT FacDept, SUM(FacSalary)


FROM Faculty
GROUP BY FacDept;

➢ SELECT FacDept, MAX(FacSalary)


FROM Faculty
GROUP BY FacDept;
➢ SELECT FacDept, MIN(FacSalary)
FROM Faculty
GROUP BY FacDept;

➢ SELECT FacDept, AVG(FacSalary)


FROM Faculty
GROUP BY FacDept;

➢ SELECT FacDept, COUNT(FacSSN)


FROM Faculty
GROUP BY FacDept;

SQL Aliases: SQL aliases are used to give a table, or a column in a table, a temporary name Aliases are
often used to make column names more readable. An alias only exists for the duration of the query.

Alias Column Syntax:

SELECT column_name AS alias_name


FROM table_name;

Example:
➢ SELECT FacDept, AVG(FacSalary) AS AverageSalary
FROM Faculty
GROUP BY FacDept;

The SQL HAVING Clause: The HAVING clause was added to SQL because the WHERE keyword
could not be used with aggregate functions.

Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Example:
➢ SELECT FacDept, AVG(FacSalary) AS AverageSalary
FROM Faculty
GROUP BY FacDept
HAVING FacSalary > 50000.

You might also like