Skip to content

Instantly share code, notes, and snippets.

@PrototypeAlex
Created September 13, 2012 05:35
Show Gist options
  • Save PrototypeAlex/3712107 to your computer and use it in GitHub Desktop.
Save PrototypeAlex/3712107 to your computer and use it in GitHub Desktop.
SQL LITE SOT
create table cities (
code varchar(3) primary key,
name text
);
create table people (
email text check ( email like '%@%' ),
gender text not null check ( gender in ('M', 'F') ),
name text not null,
password text,
age int not null,
city_code varchar(3) references cities(code),
primary key (email)
);
INSERT INTO cities VALUES ( 'wlg', 'Wellington' );
INSERT INTO cities VALUES ( 'akl', 'Auckland' );
INSERT INTO cities VALUES ( 'chc', 'Christchurch' );
INSERT INTO people (email, gender, name, age, city_code)
VALUES ( 'blanketman@gmail.com', 'M',
'Ben Hana', 38, 'wlg' );
INSERT INTO people (email, gender, name, age, city_code)
VALUES ( 'helen@gmail.com', 'F',
'Helen Clark', 58, 'wlg' );
INSERT INTO people (email, gender, name, age, city_code)
VALUES ( 'john@key.co.nz', 'M',
'John Key', 47, 'akl' );
INSERT INTO people (email, gender, name, age, city_code)
VALUES ( 'kerry@hotmail.com', 'F',
'Kerry Prendergast', 63, 'chc' );
SELECT * from cities;
SELECT name, age
FROM people
WHERE gender = 'M'
AND age BETWEEN 20 AND 40;
SELECT *
FROM people
WHERE gender = 'F';
CREATE VIEW women
AS
SELECT email, name, password, age, city_code
FROM people
WHERE gender = 'F';
CREATE VIEW men
AS
SELECT email, name, password, age, city_code
FROM people
WHERE gender = 'M';
SELECT *
FROM men
JOIN women;
SELECT men.name, women.name
FROM men, women
WHERE men.city_code = women.city_code AND
CASE WHEN men.age > women.age
THEN women.age > ((men.age / 2) + 7)
ELSE men.age > ((women.age / 2) + 7)
END;
UPDATE people
SET password = substr( name, 0, 5 );
UPDATE people
SET password = 'waka'
WHERE email = 'blanketman@gmail.com';
DELETE FROM people
WHERE email = 'blanketman@gmail.com';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment