Unit 12 SQL and Databases
SQL General notes and Terminology.doc
Remember: Capital letters don’t matter, only spacing and spelling
SELECT <Field name>, <Field name 2> //* means all. So you can say SELECT*
FROM <Table name>
WHERE <Field name> <condtion> //eg: WHERE ReplacementValue between 100
ORDER BY <Field name> DESC/ASC, <Field name 2> DESC/ASC; //etc
DISTINCT
Unique fields are only displayed
WHERE condition clause
Unit 12 SQL and Databases 1
This is for the WHERE condition
<> or !=(without spaces- my phone is autocorrecting the sign
here is an explanation(make sure this is in your school profile)
https://www.ssir.co.za/mod/resource/view.php?id=10447
Limiting the Rows Selected for a query
to limit we use the WHERE condition
Unit 12 SQL and Databases 2
BETWEEN…AND…
IS NULL or IS NOT NULL
LIKE(Compares substrings and strings)
INSERT INTO
Unit 12 SQL and Databases 3
AND/OR Clause
Unit 12 SQL and Databases 4
Video: https://www.ssir.co.za/mod/resource/view.php?id=10448
For more just look at this Week https://www.ssir.co.za/course/view.php?
id=5§ion=17
Updating Databases
UPDATE <Table Name>
SET <Field name> = <value1>, <Field name 2>=Field Name
WHERE <Field Name> <condition>
Inserting Databases
Unit 12 SQL and Databases 5
Creating an ALIAS(AS)
OR for multiple words, you put square brackets. Its good practice to not use
that and continue using the camel case convention
Unit 12 SQL and Databases 6
SELECT YEAR(fieldname) as [Year]
Select Description, UnitPrice, UnitPrice*1.05 as [NewPrice]
From tblItems
Aggregate functions(with AS alias function)
Aggregate means together, so it will display one record
SELECT <COST/SUM/AVG/MAX/MIN/COUNT>(field name) AS <New field na
FROM <Table name>
note: Count shows how many records are in the field. If a field is missing a
value it wont count it
HAVING-allows you to place a condition onto the Aggregate function
GROUP BY
The GROUP BY clause is used in SQL queries to group rows(records) that
have the same values in specified columns. This is particularly useful when
working with aggregate functions, as it allows you to perform calculations on
each group separately.
Example scenario
For example, you could use GROUP BY to find the total sales for each
product category, or the average salary for each department in a
company.
The GROUP BY clause is often used in conjunction with aggregate functions
like SUM, COUNT, AVG, MAX, or MIN. Here's a basic syntax example:
Unit 12 SQL and Databases 7
SELECT field1, field2, AGG_FUNCTION(field3)
FROM table_name
GROUP BY field1, field2;
This would group the results by field1 and field2, then apply the aggregate
function to field3 for each group of records.
Date and Time Function
SELECT*
FROM <tblName>
WHERE <fieldNameOfDateField> = or < or > #insertdate#
This makes a new field!
SELECT DATE()
Unit 12 SQL and Databases 8
Unit 12 SQL and Databases 9