H2-Worksheet.md 6.3KB

SQL Worksheet

Proceed through the sections below, testing out the queries shown and observing the result. Use the included H2 console to run your queries. Feel free to experiment with your own variations on these queries. This will help to build your familiarity with the process of working with SQL queries.

Clauses

Select Clause
From Clause

Insert people into People table

INSERT INTO PEOPLE (LAST_NAME, FIRST_NAME, MOBILE, BIRTHDAY)
VALUES ('Smith', 'John', '230-4293', '1973-01-23');

Following the example, insert at least three other people into the table.

Selecting all rows from table

SELECT * FROM PEOPLE;

Updating rows

Update firstname for person whose id is 1

UPDATE PEOPLE SET FIRST_NAME = 'TONY' WHERE ID = 1;

Update mobile where last names are Smith

UPDATE PEOPLE SET MOBILE = '152-9854' WHERE LAST_NAME = 'Smith';

Update multiple columns with multiple conditions

UPDATE people SET birthday = '1955-01-25' 
WHERE 
	lastName = 'Smith' 
	AND id = 4;
UPDATE people SET mobile = '333-3333', lastName = 'Johnson' 
WHERE firstName = 'Noelle' OR firstName = 'Raj';

Basic Functions

SELECT * FROM PEOPLE;
SELECT COUNT(HOMEPHONE) FROM HOMES;
SELECT HOMENUMBER FROM HOME WHERE ID = 1;
SELECT COUNT(*) FROM HOME;
SELECT COUNT(DISTINCT lastName) FROM PEOPLE;
SELECT  SUM(ID), AVG(ID) FROM PEOPLE;
SELECT SUM(ID) AS sum, AVG(ID) AS avg FROM PEOPLE;
SELECT MIN(birthday) FROM PEOPLE;

Strings

SELECT UPPER (FRIST_NAME), LOWER(LAST_NAME) FROM PEOPLE;
SELECT REPLACE(LAST_NAME, 'a', '1') FROM PEOPLE;
SELECT LAST_NAME FROM PEOPLE;
INSERT INTO people (FIRST_NAME, LAST_NAME, MOBILE) 
VALUES ('Otto', 'Von Count', '656-6548');
SELECT CONCAT(FIRST_NAME, LAST_NAME) FROM people
WHERE LAST_NAME = 'Smith'
SELECT CONCAT(firstName, ' ', lastName) 
FROM people 
WHERE lastName = 'Smith'
SELECT CONCAT_WS(' ',firstName, lastName, mobile) 
FROM people WHERE lastName= 'Smith'
SELECT HOMENUMBER, LEFT(HOMENUMBER, 3), RIGHT(HOMENUMBER, 2) FROM HOME
SELECT LENGTH(address), CHAR_LENGTH(address) FROM HOME;
CREATE TABLE length_test (string varchar(10) );
INSERT INTO length_test VALUES ('$'),('€');
SELECT string, LENGTH(string), CHAR_LENGTH(string) FROM length_test

Compare

SELECT firstName, lastName, YEAR(birthday) FROM people WHERE birthday >= '1970-07-06' AND birthday<='1987-07-06';
SELECT firstName, birthday FROM people WHERE firstName='Thomas' OR firstName='Raj' OR firstName='Sheeri';
SELECT firstName, birthday FROM people WHERE firstName IN ('Noelle', 'Thomas', 'Raj');

Wild Cards

SELECT firstName FROM PEOPLE WHERE RIGHT(firstName,1)='e';
SELECT firstName FROM people WHERE firstName LIKE '%j'; 
SELECT firstName FROM people WHERE firstName LIKE '%o%';
SELECT firstName FROM people WHERE firstName NOT LIKE '%o%';
SELECT COUNT(*) FROM PEOPLE
SELECT lastName, COUNT(*) FROM people GROUP BY lastName;
SELECT lastName, GROUP_CONCAT(mobile) FROM PEOPLE GROUP BY lastName;
SELECT lastName, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people GROUP BY lastName;
SELECT lastName, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people GROUP BY lastName  HAVING COUNT(*)>1;
SELECT lastName, GROUP_CONCAT(mobile SEPARATOR ' and ') FROM people WHERE lastName != 'Cabral' GROUP BY lastName  HAVING COUNT(*)>1;

Sorting

SELECT firstName, birthday FROM people ORDER BY birthday;
SELECT firstName, birthday FROM people ORDER BY birthday DESC;
SELECT firstName, lastName FROM people ORDER BY lastName, firstName;
SELECT firstName, birthday FROM people ORDER BY birthday DESC LIMIT 3;
SELECT firstName, MONTHNAME(birthday) as mon, birthday FROM people ORDER BY MONTH(birthday);
SELECT lastName, COUNT(*) FROM  people GROUP BY lastName;
SELECT lastName, COUNT(*) FROM  people GROUP BY lastName ORDER BY NULL;

Inserting and Replacing Records

INSERT INTO people (firstName, lastName, birthday, home_id)
	VALUES
	('John', 'Smith', '1998-04-07', 4),
	('Maya', 'Wasserman' , NULL, 4),
	('Paul', 'Thompson', '1996-05-27', 1);

Replace

DELETE FROM people WHERE firstName='Maya';
SELECT * FROM people;
MERGE INTO people (firstName, lastName, birthday, home_id)
	VALUES
	('John', 'Sharma', '1998-04-07', 1),
	('Paul', 'Sharma', '1996-05-27', 4),
	('Maya', 'Wasserman', '1900-01-05',1);

JOIN

INSERT INTO people (firstName, lastName, birthday)
	VALUES ('Eli', 'Kramer', '1984-01-15');
SELECT * FROM people;
SELECT * FROM home;
SELECT p.firstName, h.address 
FROM PEOPLE p
INNER JOIN HOME h  on (p.HOME_ID = h.ID)
SELECT firstName, lastName
FROM PEOPLE p
INNER JOIN HOME h  on (p.HOME_ID = h.HOME_ID)
WHERE p.HOME_ID = 1
SELECT p.*, h.address, h.homenumber
FROM PEOPLE p
INNER JOIN HOME h  on (p.HOME_ID = h.HOME_ID)
WHERE p.firstName  LIKE '%e%'
Exercise:

Devise a report

show all the people in your address table
only if you know their birthday
show name, address and birthday
order by month, so January birthdays are first

Devise a report

Output all information for all people and their home information

UNION

The UNION operator is used to combine the result-set of two or more SELECT statements.

Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.

SELECT column_name(s) FROM table1

UNION

SELECT column_name(s) FROM table2;

Note: The UNION operator selects only distinct values by default. To allow duplicate values, use the ALL keyword with UNION.

SELECT column_name(s) FROM table1

UNION ALL

SELECT column_name(s) FROM table2;

Example
SELECT HOME_ID FROM People
UNION
SELECT HOME_ID FROM Home
ORDER 	BY HOME_ID; 
SELECT HOME_ID FROM People
UNION ALL
SELECT HOME_ID FROM Home
ORDER 	BY HOME_ID;