PSU 2012. SQL: Introduction. SQL: Introduction. Relational Databases. Activity 1 Examining Tables and Diagrams



Similar documents
Database Extensions, Page Fragments and Plugins

Relational Database: Additional Operations on Relations; SQL

Creating QBE Queries in Microsoft SQL Server

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Oracle Database: SQL and PL/SQL Fundamentals NEW

Using SQL Queries in Crystal Reports

Access Queries (Office 2003)

SQL Server 2008 Core Skills. Gary Young 2011

Advanced Query for Query Developers

The software shall provide the necessary tools to allow a user to create a Dashboard based on the queries created.

Welcome to the topic on queries in SAP Business One.

Oracle SQL. Course Summary. Duration. Objectives

Oracle Database: SQL and PL/SQL Fundamentals

Lab # 5. Retreiving Data from Multiple Tables. Eng. Alaa O Shama

Oracle Database 10g: Introduction to SQL

Advanced SQL Queries for the EMF

Oracle Database: SQL and PL/SQL Fundamentals

Creating Bulk Rosters from PowerSchool to Allow Staff Access to MEA/SBAC Score Results in the Online Reporting System (ORS)

Import and Export User Guide. PowerSchool 7.x Student Information System

SQL SELECT Query: Intermediate

Beginning C# 5.0. Databases. Vidya Vrat Agarwal. Second Edition

Module 9 Ad Hoc Queries

Access Database Design

School & District Data: Analysis Spreadsheets

Inquiry Formulas. student guide

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

SES Project v 9.0 SES/CAESAR QUERY TOOL. Running and Editing Queries. PS Query

1 Structured Query Language: Again. 2 Joining Tables

Toad for Data Analysts, Tips n Tricks

Lecture 25: Database Notes

Query 4. Lesson Objectives 4. Review 5. Smart Query 5. Create a Smart Query 6. Create a Smart Query Definition from an Ad-hoc Query 9

Ad Hoc Advanced Table of Contents

Personal Geodatabase 101

Health Functions in PowerSchool

Microsoft Access 2007

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Microsoft Office Access 2007 which I refer to as Access throughout this book

Oracle Database 12c: Introduction to SQL Ed 1.1

Instant SQL Programming

Excel: Analyze PowerSchool Data

Monthly Payroll to Finance Reconciliation Report: Access and Instructions

Microsoft Access 3: Understanding and Creating Queries

Schedule User Guide. PowerSchool 6.0 Student Information System

Data Mining Commonly Used SQL Statements

Produced by Flinders University Centre for Educational ICT. PivotTables Excel 2010

Choose the Reports Tab and then the Export/Ad hoc file button. Export Ad-hoc to Excel - 1

The join operation allows you to combine related rows of data found in two tables into a single result set.

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Microsoft' Excel & Access Integration

LMS User Manual LMS Grade Book NUST LMS

Business Objects 4.1 Quick User Guide

How To Create A Powerpoint Intelligence Report In A Pivot Table In A Powerpoints.Com

Database Administration with MySQL

Automated Walk-In Scheduling. Prior to Using Automated Walk-In Scheduling. The Scheduling Engine

MySQL for Beginners Ed 3

Access Tutorial 3 Maintaining and Querying a Database. Microsoft Office 2013 Enhanced

Cal Answers Analysis Training Part I. Creating Analyses in OBIEE

Tutorial 3 Maintaining and Querying a Database

Check out our website!

Introduction to Microsoft Jet SQL

Foundations & Fundamentals. A PROC SQL Primer. Matt Taylor, Carolina Analytical Consulting, LLC, Charlotte, NC

Creating and Using Databases with Microsoft Access

Introduction to SQL for Data Scientists

A basic create statement for a simple student table would look like the following.

Access 2010: Creating Queries Table of Contents INTRODUCTION TO QUERIES... 2 QUERY JOINS... 2 INNER JOINS... 3 OUTER JOINS...

DBMS / Business Intelligence, SQL Server

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Excel 2003: Ringtones Task

Intro to Mail Merge. Contents: David Diskin for the University of the Pacific Center for Professional and Continuing Education. Word Mail Merge Wizard

Query. Training and Participation Guide Financials 9.2

Getting Started Guide

Tips and Tricks SAGE ACCPAC INTELLIGENCE

Business Objects. Report Writing - CMS Net and CCS Claims

When a variable is assigned as a Process Initialization variable its value is provided at the beginning of the process.

Oracle Database: SQL and PL/SQL Fundamentals NEW

Programming with SQL

Information Systems SQL. Nikolaj Popov

MS Excel. Handout: Level 2. elearning Department. Copyright 2016 CMS e-learning Department. All Rights Reserved. Page 1 of 11

Kaseya 2. Quick Start Guide. for VSA 6.3

HansaWorld SQL Training Material

Tutorial 3. Maintaining and Querying a Database

Microsoft Office 2010

SQL Server An Overview

Microsoft Access Basics

School account creation guide

Getting Started with Access 2007

Oracle 10g PL/SQL Training

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

MICROSOFT ACCESS 2003 TUTORIAL

What is a Mail Merge?

Windows XP File Management

Chapter 1 Overview of the SQL Procedure

Parent Single Sign-On Quick Reference Guide

9.1 SAS. SQL Query Window. User s Guide

PSU Winter Training Roadmap

Cognos Event Studio. Deliver By : Amit Sharma Presented By : Amit Sharma

BID2WIN Workshop. Advanced Report Writing

Database Query 1: SQL Basics

Transcription:

PSU 2012 SQL: Introduction SQL: Introduction The PowerSchool database contains data that you access through a web page interface. The interface is easy to use, but sometimes you need more flexibility. As your database grows, you may want to search in a more detailed manner, or access data from across schools, and the interface is not the easiest way to do that. With SQL (Structured Query Language), you can make detailed queries across the whole database. By the end of this session, you will be able to: Describe the basic database structure of PowerSchool Use SQL to write basic queries with filters and simple joins of two tables Use SQL with PowerViews Relational Databases Spreadsheets are used to collect and organize data. Columns contain fields, and rows contain instances of the fields. Schools could use spreadsheet programs like Excel to store and organize student data, but the spreadsheets would be too large to work with. Relational databases work much like a set of interlinked spreadsheets. They are sets of tables that can be connected by data shared across the tables. For example, in PowerSchool, the Students table connects to the StoredGrades table using a student ID that they have in common. The tables have different names for the data (ID in the Student table and StudentID in the Stored Grades table), but the data is the same. In these tables, the columns represent fields, and the rows are filled with instances. To read the tables, go down to the row you want, and then read across to find all the data for that instance. For example, in the Students table, read down until you find the correct last and first name, and then read across to find grade level, gender, and other information about the student. You can make database diagrams to show the relationships between tables. In these diagrams, tables are represented by boxes filled with their field names and connected by lines to show how they link. You can find information on PowerSchool s data structure of 300 tables in the Data Dictionary, available through PowerSource. Find some of the more common tables by going to Direct Database Export (DDE). Access DDE by clicking System > Direct Database Export (DDE). Activity 1 Examining Tables and Diagrams In this activity, practice one of the two ways of finding information about PowerSchool s data structure, and see how some tables are related. 1. Using the Database Dictionary or DDE (Direct Database Export), find the Students and CC tables. What fields link the two tables? 2. If you want to make a list of letter grades, student names, course names, gender, and grade levels from one school and one term, what fields should you use from the database? Answer: 3. Examine the database diagram in the slide shown. What is the relationship between the tables as pictured in the diagram? Answer:

Why Use SQL? SQL is a powerful tool, but sometimes a power saw is too much, and a paring knife will work. So how do you decide which tool to use Quick Export, DDE, or SQL? It depends on the details of the job. If you want just a few fields from the Students table, then it makes sense to use Quick Export or DDE. You can join two tables in Quick Export and DDE, but joining tables in SQL is easier. The Basics of SQL SQL is the language of databases. Your PowerSchool license allows you to construct SQL statements called queries to pull data directly from the database. This course covers the basics of how to write these queries. SQL queries have three main components the SELECT clause (get what data?), the FROM clause (from what tables?), and the final clause, which holds special instructions (like filter or order the data). To get data from more than one table, add a JOIN statement and specify where to match the rows in the different tables (ON). For each table you add to the query, an additional JOIN is required. In a query of two tables, use one JOIN; in a query drawing from three tables, use two JOINs; and so on. You can also sort data and filter data using the SQL commands ORDER BY and WHERE. Other SQL calculations and formatting changes are covered in the Advanced SQL class. When typing SQL queries, the convention is to type SQL commands in all caps. The database doesn t require capitalized commands, but using all caps makes reading the queries or checking them for errors easier. When joining tables, indicate the tables from which the fields in your SELECT clause are coming. In the SELECT clause, type each field name preceded by the appropriate table name, separated by a period. This syntax tells the query which tables to scan that contain the field names and information you want. Activity 2 Writing SQL Queries You ll build several SQL queries, working sequentially and adding complexity. Open SQL Developer, connect to your assigned server, and open the query pane if it does not open automatically. For this activity, analyze the distribution of letter grades in your student population. 1. To start, type this basic query: SELECT lastfirst WHERE schoolid=100 Click Execute Statement using the green right-facing arrow above Enter SQL Statement. What did the query return? 2. Add gender and grade level to the query by putting their field names in the SELECT clause: SELECT lastfirst, gender, grade_level WHERE schoolid=100 Click Execute Statement. What did the query return? 3. Join the StoredGrades table to the query by adding a JOIN statement and an ON statement: Copyright 2012 Pearson Page 2

SELECT students.lastfirst, students.gender, students.grade_level JOIN storedgrades ON students.id=storedgrades.studentid WHERE students.schoolid=100 The table name goes after the JOIN, and the fields to use for the join go after the ON. Specify which tables each field comes from when making a join. 4. Add the course_name, grade, and termid fields from the StoredGrades table to the SELECT statement. Also, add termid to the WHERE filter, and use the ORDER BY command to sort by last name: SELECT students.lastfirst, students.gender, students.grade_level, storedgrades.course_name, storedgrades.grade, storedgrades.termid JOIN storedgrades ON students.id=storedgrades.studentid WHERE students.schoolid=100 AND storedgrades.termid>=2200 ORDER BY students.lastfirst Typing every field name preceded by its table name can be tedious, especially when the query is long and complex. To shorten your query, use an alias. To make an alias, type the letter or letters you want to use for the table name after the official name of the table in the FROM statement. For example, if you want the letter "s" to stand for the Students table, define "s" as the alias in the FROM statement like this: s. You can also create an alias for a field to rename columns in the output data. 5. To make the above statement and its output easier to read, make aliases for the tables and rename the grade field from the StoredGrades table as "Letter Grade." SELECT s.lastfirst, s.gender, s.grade_level, sg.course_name, sg.grade "Letter Grade", sg.termid s JOIN storedgrades sg ON s.id=sg.studentid WHERE s.schoolid=100 AND sg.termid>=2200 ORDER BY s.lastfirst 6. To save this query, use the File menu and choose Save. Give the query a logical name. To begin a new query, clear the command window, or choose File > New, and select SQL file. When working on a grant proposal, generate teacher demographic data by school. 7. Start with a query of the Teachers table; use aliases for the table names. SELECT t.lastfirst, t.ethnicity FROM teachers t 8. Join the Schools table to your query, matching schoolid from the Teachers table to school_number from the Schools table, and add s.name to your select statement. SELECT t.lastfirst, t.ethnicity, s.name FROM teachers t JOIN schools s ON t.schoolid=s.school_number Save this query and open another. Copyright 2012 Pearson Page 3

Now you need a count of students by school and enrollment status. 9. Start by leaving the SELECT clause vague, and joining together the tables you need. SELECT s.lastfirst FROM schools sc JOIN students s ON s.schoolid=sc.school_number The GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into groups. It is also used in conjunction with aggregate functions such as AVG, MAX, MIN, SUM, and COUNT. The GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the ORDER BY clause, if used. GROUP BY differs from ORDER BY, which places the SELECT results in the proper sort order. 10. Since you want a count by two grouping factors, add in a GROUP BY clause. This will return results grouping each school by name and the number of active students enrolled. SELECT COUNT(s.id) FROM schools sc JOIN students s ON s.schoolid=sc.school_number GROUP BY sc.name, s.enroll_status 11. Specify the data to display. Add a count and include the fields in the GROUP BY clause. Add an ORDER BY clause to sort by enroll_status values. SELECT COUNT(s.id), sc.name, s.enroll_status FROM schools sc JOIN students s ON s.schoolid=sc.school_number GROUP BY sc.name, s.enroll_status ORDER BY s.enroll_status What does the resulting data look like? How does it change if you reverse the order of the fields in the GROUP BY? 12. Save the results data output to your desktop. Name the file Student Count. Locate the file and change the file extension to.txt. Open the file with your spreadsheet program to work with it further. PowerViews PowerViews are what PowerSchool calls views. Views are not actual tables but rather the result of complex SQL queries designed by developers to make querying and reporting easier. Views are based on pre-built SQL queries that are run automatically by the database engine. They are dynamic and change as the tables they are built from change. PowerViews combine the most requested data elements into a single table view. This pre-queried data gives you easy access to information without having to create complex SQL queries. You can query PowerViews as you would a table, but their names are long and unusual. You can find information on the different PowerViews and what data they contain in the PowerViews Data Dictionary. You can write queries of PowerViews as though they were regular tables, except that you must insert ps. in front of the name. Some data is more accessible in PowerViews than in the regular tables. This is particularly true of data in the Gen table or custom field data. Using PowerViews to get data from custom fields is covered in the Advanced SQL class. Copyright 2012 Pearson Page 4

Activity 3 Using PowerViews in SQL Queries Enhance some of your previous queries with data from PowerViews. These activities are only a sample of what you can do with PowerViews. 1. With PowerViews, you can add ethnicity data to an analysis of grades more easily. Add in a JOIN to Student Demographics, and then add the ethnicity name to the SELECT clause. SELECT s.lastfirst, s.gender, s.grade_level, d.ethnicity_name, sg.course_name, sg.grade "Letter Grade", sg.termid s JOIN storedgrades sg ON s.id=sg.studentid JOIN ps.pssis_student_demographics d ON d.student_number=s.student_number WHERE s.schoolid=100 AND sg.termid>=2200 ORDER BY s.lastfirst You can also add Special Programs enrollments using the correct PowerView. But because not every student has a record in that PowerView, you must use a special type of JOIN. There are two types of SQL JOINs INNER JOINs and OUTER JOINs. An INNER JOIN is the most common join and represents the default join type. If you don't put INNER or OUTER in front of the SQL JOIN keyword, then INNER JOIN is used. The LEFT OUTER JOIN selects all the valid rows from the first table listed after the FROM clause, and any valid rows that have matches in the second table. So in this case, use a LEFT OUTER JOIN to get all student records and any matching Special Programs enrollment records. LEFT and RIGHT JOINs are explained in greater detail in the Advanced SQL course. 2. Add a LEFT OUTER JOIN to the Special Programs Enrollment PowerView. SELECT s.lastfirst, s.gender, s.grade_level, d.ethnicity_name, sp.program_name, sg.course_name, sg.grade "Letter Grade", sg.termid s JOIN storedgrades sg ON s.id=sg.studentid JOIN ps.pssis_student_demographics d ON d.student_number=s.student_number LEFT OUTER JOIN ps.pssis_special_pgm_enrollments sp ON sp.student_number=s.student_number WHERE s.schoolid=100 AND sg.termid>=2200 ORDER BY s.lastfirst How would the results differ if you used an INNER JOIN? Copyright 2012 Pearson Page 5

Key Points Primary Keys and Foreign Keys Define table relationships Quick Export, DDE, or SQL Use the right tool Basic SQL SELECT [somestuff] FROM [sometable] WHERE Filters search results JOIN Combines multiple tables together with ON statements PowerViews Saves time by connecting frequently used data into one virtual table Copyright 2012 Pearson Page 6