YASC DC 2019 RE110 SQL Basics
YASC DC 2019 RE110 SQL Basics
In this section:
Relational Databases
Voyager Schema
Basic SQL Syntax and Operators
Advanced SQL Operations
Relational Databases
Structure of a Relational Database
• Information is stored in a database using Tables that are comprised of Rows & Columns.
• Each table is a series of Rows (also known as records) which are identified with a Primary Key. A
Primary Key (also known as Handle) is a unique identifier for that row.
• Yardi calls these unique identifiers HMY.
• A Column represents each field of data in a Row.
In the result set above, we see that Property comind01 has an hMy of 1
This material contains Yardi confidential and other information that is protected from public disclosure and subject to copyright, trademark and other intellectual
property protections. Your access to and use of this information is strictly subject to the terms of your software licensing or other agreement with Yardi, including
any agreement of non-disclosure. You may not record, copy, transmit, redistribute or otherwise in any way disclose, in whole or in part, this material except as
expressly permitted by your software licensing or other agreement of non-disclosure, or as authorized by Yardi in writing.
Any client data contained in this material is used with permission and is owned and controlled exclusively by the client.
Yardi, the Yardi Logo, the names of Yardi products and services, and any other registered or unregistered trademarks belong to Yardi Systems, Inc. and are
protected by the laws of the United States and may be protected as trademarks in other countries. All other trademarks and service marks belong to their
respective owners.
Table Relationships
Relationships between tables are defined by Foreign Keys. A Foreign Key points from one table
to the Primary Key of another table.
In the above example, the Property table’s HMY is equal to the Unit table’s HPROPERTY.
Property.hMy = Unit.hProperty
• The Property.hMy & Unit. hMy are the Primary Keys (unique identifiers) for the two tables.
• The Unit.hProperty is the foreign key (pointer) to the Property.hMy.
Data Types
• char(size) - Fixed-length character string. Size is specified in parenthesis. Max 255
bytes\characters.
• varchar(size) - Variable-length character string. Max size is specified in parenthesis.
• numeric(size) - Number value with a max number of column digits specified in parenthesis.
• decimal (size,d) - Number value with a maximum number of digits of "size" total, with a
maximum number of "d" digits to the right of the decimal. Eg(21,2 for dollars and 21,6 for
percent.)
• Date - Date value
Voyager Schema
Small Sample of Voyager Hierarchy
Page 2 of 9
Yardi Advanced Solutions Conference
RE110 - SQL & Database Schema
SELECT statement
1) SELECT (fields)
2) FROM (tables)
3) WHERE (record-level filter conditions)
• SELECT * FROM Table – Returns all fields and all rows from a table.
• SELECT field1, field2, field3 FROM Table ‒ Returns only selected field (Fields are separated by
commas)
Relational Operators
• = Equal
• < > Not Equal
• < Less Than
• > Greater Than
• <= Less Than or Equal To
• >= Greater Than or Equal To
Page 3 of 9
Yardi Advanced Solutions Conference
RE110 - SQL & Database Schema
Arithmetic Operators
• + Add
• - Subtract
• * Multiply
• / Divide
Aggregate Functions
• Count - SELECT count(hMy) FROM acct
• Sum - SELECT sum(sRent) FROM tenant
• Avg - SELECT avg(sRent) FROM tenant
• Max - SELECT max(sRent) FROM tenant
• Min - SELECT min(sDeposit0) FROM tenant
Nulls
• Evaluating
o SELECT sCode FROM property WHERE sState is null
o SELECT sCode FROM property WHERE sState is not null
• Replacing nulls
o SELECT sLastName, isnull(sZipCode,'No Zip in DB') FROM tenant
o ***NOTE isNull is a SQL Server only function
Compound Conditions
• AND – Joins two or more conditions. Returns a row if the data satisfies ALL conditions.
o SELECT sLastName, dtMoveIn FROM tenant WHERE sRent >1000 AND dtMoveIn > ‘2014-
01-01’
• OR – Joins two or more conditions. Returns a row if the data satisfies ANY of the conditions.
o SELECT sLastName, sRent, dtMoveIn FROM tenant WHERE sRent < 1000 OR dtMoveIn
<‘2014-01-01’
Page 4 of 9
Yardi Advanced Solutions Conference
RE110 - SQL & Database Schema
In & Between
• To find all properties in a list of states.
SELECT sCode, sAddr1
FROM property
WHERE sState IN ('CA','CO')
• To find tenants whose rents are greater than or equal to 800 and less than or equal to 1500.
SELECT sLastName
FROM tenant
WHERE sRent BETWEEN 800 and 1500
• To find all properties not in a list of states.
SELECT sCode, sAddr1
FROM property
WHERE sState NOT IN ('CA','CO')
• To find tenants whose rents are not greater than or equal to 800 and not less than or equal to
1500.
SELECT sLastName
FROM tenant
WHERE sRent NOT BETWEEN 800 and 1500
Using Like
• To find all tenant LastNames that start with an ‘S’
SELECT sLastName
FROM tenant
WHERE slastname LIKE ‘S%’
• ‘%S’ – would be ends in S.
• ‘%S%’ – an ‘S’ anywhere in the field
• Not like ‘S%’ – all names that do not start with S
Page 5 of 9
Yardi Advanced Solutions Conference
RE110 - SQL & Database Schema
Alias in SQL
• Shorthand to make the SQL a little simpler but it is a double edged sword…
Order By
• Sort Rows with the ORDER BY clause
o ASC ascending order, default (0-9, A-Z)
o DESC descending order
o ORDER BY clause is last in the SELECT command
• Using Multiple ORDER BYs
o Separate each one with a comma
o Each column can have its own order specified
SELECT sCode, hProperty, hUnit
FROM tenant
ORDER BY hProperty DESC, hUnit ASC
• Can also order by column position, but this is harder to read
SELECT sCode,sRent
FROM tenant
ORDER BY 2
Group By
• Used to divide rows into smaller groups.
• Use the group functions to return summary information for each group.
• Used to combine aggregate columns and regular columns in the same query.
• Add an ORDER BY to sort GROUP BYs
• Must Include only the non-aggregate columns in the GROUP BY
SELECT sState, count(*)
FROM Property
GROUP BY sState
ORDER BY sState
Page 6 of 9
Yardi Advanced Solutions Conference
RE110 - SQL & Database Schema
Having Clause
• Use the HAVING Clause to specify which groups are to be displayed.
SELECT sState, count(*)
FROM property
WHERE itype=3
GROUP BY sState
HAVING count(*) >2
• The example above returns the State and the count within the state where the property type is
a property (not a property list!) from those States with more than 2 properties
• Step 1 – Rows are grouped
• Step 2 - The group function is applied to the group
• Step 3 - Groups matching the HAVING condition are displayed
Page 7 of 9
Yardi Advanced Solutions Conference
RE110 - SQL & Database Schema
Inner Joins
• Used when you wish to return information when records exist in BOTH tables.
• Example:
SELECT *
FROM property p
INNER JOIN unit u On p.hMy = u.hProperty
where p.sState = ‘CA’
• The above query will return data from the Property table ONLY if the properties have unit
records and the property state is CA.
Page 8 of 9
Yardi Advanced Solutions Conference
RE110 - SQL & Database Schema
Tips
• Use BOL (Books Online) - In SQL Server Mgmt Studio F1 key
• Yardi’s SQL Scripting Guide (available on Client Central)
• SP_Help and SP_Tables within SQL Server
• Google It!
Page 9 of 9