Skip to content

Instantly share code, notes, and snippets.

View youjingwong's full-sized avatar

You Jing Wong youjingwong

View GitHub Profile
#1. Count the votes for Sen. Olympia Snowe, whose id is 524.
SELECT COUNT(*) FROM votes WHERE politician_id = 524 ;
#2. Now do that query with a JOIN statement without hard-coding the id 524 explicitly, querying both the votes and congress_members table.
SELECT COUNT(*) FROM congress_members INNER JOIN votes ON congress_members.id = votes.politician_id WHERE congress_members.name = "Sen. Olympia Snowe" ;
#3. How about Rep. Erik Paulsen? How many votes did he get?
SELECT COUNT(*) FROM congress_members INNER JOIN votes ON congress_members.id = votes.politician_id WHERE congress_members.name = "Rep. Erik Paulsen" ;
#4. Make a list of Congress members that got the most votes, in descending order. Exclude the create_at and updated_at columns.
.container {
/*
If you have a block-level element of a certain width, margin: 0 auto;
will how you center it inside its parent container.
See: http://bluerobot.com/web/css/center1.html
Don't change the container width.
*/
margin: 0 auto;
@youjingwong
youjingwong / Cohort.rb
Last active August 29, 2015 14:07
ActiveRecord, Jr. 2: SQL Be Gone
class Cohort < Database::Model
def self.length
all.length
end
self.attribute_names = [:id, :name, :created_at, :updated_at]
attr_reader :attributes, :old_attributes
@youjingwong
youjingwong / cohort.rb
Created October 16, 2014 06:27
ActiveRecord, Jr. 1: A Basic ORM
class Cohort < Database::Model
def self.all
Database::Model.execute("SELECT * FROM cohorts").map do |row|
Cohort.new(row)
end
end
def self.length
all.length
end
@youjingwong
youjingwong / gist:e5ea876ff874960ae869
Created October 16, 2014 02:04
Student Roster DB from Schema
require 'sqlite3'
$db = SQLite3::Database.new "students.db"
module DBManipulate
def self.add(first_name, last_name, gender, birthday, email, phone)
$db.execute(
<<-SQL
INSERT INTO students
(first_name, last_name, gender, birthday, email, phone, created_at, updated_at)
http://minus.com/i/bcPwh2BjwNXLY
<?xml version="1.0" encoding="utf-8" ?>
<!-- SQL XML created by WWW SQL Designer, http://code.google.com/p/wwwsqldesigner/ -->
<!-- Active URL: http://ondras.zarovi.cz/sql/demo/ -->
<sql>
<datatypes db="mysql">
<group label="Numeric" color="rgb(238,238,170)">
<type label="Integer" length="0" sql="INTEGER" quote=""/>
<type label="TINYINT" length="0" sql="TINYINT" quote=""/>
@youjingwong
youjingwong / gist:1b23445d4fd7331bcb09
Last active August 29, 2015 14:07
Poll Design Schema
http://minus.com/i/bmX1aLTg2JoD2
<?xml version="1.0" encoding="utf-8" ?>
<!-- SQL XML created by WWW SQL Designer, http://code.google.com/p/wwwsqldesigner/ -->
<!-- Active URL: http://ondras.zarovi.cz/sql/demo/ -->
<sql>
<datatypes db="mysql">
<group label="Numeric" color="rgb(238,238,170)">
<type label="Integer" length="0" sql="INTEGER" quote=""/>
<type label="TINYINT" length="0" sql="TINYINT" quote=""/>
096040001100060004504810390007950043030080000405023018010630059059070830003590007
302609005500730000000000900000940000000000109000057060008500006000000003019082040
000075400000000008080190000300001060000000034000068170204000603900000020530200000
300000000050703008000028070700000043000000000003904105400300800100040000968000200
290500007700000400004738012902003064800050070500067200309004005000080700087005109
080020000040500320020309046600090004000640501134050700360004002407230600000700450
608730000200000460000064820080005701900618004031000080860200039050000100100456200
370000001000700005408061090000010000050090460086002030000000000694005203800149500
030500804504200010008009000790806103000005400050000007800000702000704600610300500
000689100800000029150000008403000050200005000090240801084700910500000060060410000