SQL For Mis305
SQL For Mis305
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
➢ SELECT StdFirstName, StdLastName, StdCity
FROM Student
WHERE StdCity=” SEATTLE”
= Equal 3
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Example:
➢ SELECT StdFirstName, StdLastName, StdCity
FROM Student
WHERE StdCity Like ” SEATTLE”
ORDER BY Syntax:
Example:
SELECT StdFirstName, StdLastName
FROM Student
ORDER BY StdFirstName
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"
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
Example:
➢ SELECT *
FROM Student
WHERE StdCity IN(“SEATTLE”,” BOTHELL”)
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.
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.
Example:
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.
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.