0% found this document useful (0 votes)
590 views9 pages

YASC DC 2019 RE110 SQL Basics

The document discusses relational databases and the Voyager schema used by Yardi software. It provides details on database structure, table relationships, data types, and basic and advanced SQL syntax including SELECT statements, operators, aggregate functions, null values, compound conditions, and ordering results.

Uploaded by

Harish Kumar
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)
590 views9 pages

YASC DC 2019 RE110 SQL Basics

The document discusses relational databases and the Voyager schema used by Yardi software. It provides details on database structure, table relationships, data types, and basic and advanced SQL syntax including SELECT statements, operators, aggregate functions, null values, compound conditions, and ordering results.

Uploaded by

Harish Kumar
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/ 9

RE110 - SQL & DATABASE SCHEMA

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.

Structure of the Property Table – hMy = Primary Key

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.

©2018 Yardi Systems, Inc. All rights reserved.


Yardi Advanced Solutions Conference
RE110 - SQL & Database Schema

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

Yardi Schema Naming Convention

Column Prefix Definition- Datatype Examples


h Primary or Foreign Key hMy, hUnit
s String sAddr1, sAddr2
i Integer iType, iStatus
dt Date dtStamp,
dtLeaseFrom
b Boolean bInactive
d Decimal dLatePercent

Basic SQL Language Syntax

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

Examples of queries using Relational Operators


• SELECT sLastName FROM tenant WHERE sRent > 500 – Returns last name of every tenant whose
rent amount is greater than $500.
• SELECT sLastName FROM tenant WHERE dtMoveIn < ‘2013-06-01’ – Returns last name of every
tenant whose move in date is before 6/1/2013.
• SELECT sLastName FROM tenant WHERE sCode <‘b’ – Returns last name of every tenant whose
tenant code starts with ‘A’.

Arithmetic Operators
• + Add
• - Subtract
• * Multiply
• / Divide

Examples of queries using Arithmetic Operators


• SELECT sRent*12 FROM tenant ‒ Returns annualized rent
• SELECT sRent/dSqFt FROM unit ‒ Returns the monthly unit rent per SqFt
• SELECT sFirstName + ’ ‘ + sLastName FROM tenant ‒ Prints “String/Text” values together

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

Combining AND & OR


SELECT sLastName, sRent, dtMoveIn, dLeaseGrossSqft
FROM tenant
WHERE sRent < 1000
AND dtMovein <’01/01/2014’
OR dLeaseGrossSqft > 1000
• SQL looks at AND first then OR…but…
• It is better to always use parentheses with ANDs & ORs. You have to use parentheses to force
ORs to evaluate before ANDs
o SELECT sLastName, sRent, dtMoveIn, dLeaseGrossSqft
FROM tenant
WHERE srent < 1000 AND (dtMovein <‘01/01/2014‘ OR dLeaseGrossSqft >1000)

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

Final SQL Syntax


SELECT column, aggregate_function
FROM table
WHERE condition
GROUP BY group_by_expression
HAVING group_condition
ORDER BY column

Nested Select Statements


• Nested select statements can be used to easily query information that is stored in two tables.
• Without Nesting:
o SELECT hMy FROM property where scode = ‘abc123’ (Make note of the hMy to use
below)
o SELECT * FROM tenant where hProperty = 55
• With Nesting:
o SELECT * FROM tenant where hProperty in (SELECT hMy FROM property where scode =
‘abc123’)

Advanced SQL Operations


Joining Tables
• Using JOINs is an Intermediate to Advanced level topic. It is the most efficient way in SQL to join
tables together in order the query information. Most reports in Yardi applications have JOINs.
• Used to get data from more than one table
o Inner Joins: When records exist in BOTH tables
o Outer Joins: When record may not exist in both tables

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.

Left Outer Join


• Used when you wish to return information whether or not records exist in both tables.
• Example:
SELECT *
FROM property p
Left OUTER JOIN unit u on p.hMy = u.hProperty
WHERE p.sState = ‘CA’
• The above query will return ALL of the data from the Property table even if they have no Units,
as long as the property state is CA.

Date & Time Functions

Case Statements (SQL Server)


Case Tenant.iStatus
When 0 Then 'Current'
When 1 Then 'Past'
When 2 Then 'Future'

Page 8 of 9
Yardi Advanced Solutions Conference
RE110 - SQL & Database Schema

When 3 Then 'Eviction'


When 4 Then 'Notice'
Else ‘Some Other Status'
End

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

You might also like