-- test data creation
create table rgen
select 1 as n
union select 2 union select 3 union select 4 union select 5;
create table rtst
select floor(rand()*3) as rt,(rand()*1000000/100) as rs
from rgen a,rgen b,rgen c,rgen d,rgen e,rgen f,rgen g;
With the given tables, you would like to create a result similar to the one below:
+-------+-------+-------+ | 1nr | 2nr | 3nr | +-------+-------+-------+ | 26146 | 25874 | 26105 | +-------+-------+-------+ 1 row in set (0.13 sec)
Which of the queries is the most efficient?
a) select
(select count(*) from rtst where rt=0) as 1nr,
(select count(*) from rtst where rt=1) as 2nr,
(select count(*) from rtst where rt=2) as 3nr;
b) select
sum(if(rt=0,1,0)) as 1nr,
sum(if(rt=1,1,0)) as 2nr,
sum(if(rt=2,1,0)) as 3nr
from rtst;
c) select
v1.c as 1nr,
v2.c as 2nr,
v3.c as 3nr
from
(select count(*) as c from rtst where rt=0) v1,
(select count(*) as c from rtst where rt=1) v2,
(select count(*) as c from rtst where rt=2) v3;
[ Querying for Data (15%) - Using SELECT to Retrieve Data ]
a) select count(i)
from (select 1 as i union select 2 union select null) v1
where i is null;
b) select count(*)
from (select 1 as i union select 2 union select null) v1
where i is null;
c) select count(*)
from (select 1 as i union select 2 union select null) v1
where i <=> null;
d) select count(*)
from (select 1 as i union select 2 union select null) v1
where i = null;
[ SQL Expressions (15%) - NULL Values ]
a) set @a=0;
select *
from (select (@a:=@a+1) as ln,table_name
from information_schema.tables
where table_schema='mysql' order by table_name) v1
where ln in (1,2,18);
b) set @a=0;
select (@a:=@a+1) as ln,table_name
from information_schema.tables
where table_schema='mysql' and @a in (1,2,18)
order by table_name;
c) set @a=0;
select @a as ln,table_name
from information_schema.tables
where table_schema='mysql' having (@a:=@a+1) in (1,2,18)
order by table_name;
[ User Variables (5%) - User Variable Properties ]
create view rep.sales as select * from appdb.sales;
Which of the following statements are true?
a) Using views like this will decrease performance a lot because of the select *
b) Performance will not be noticeably impacted, because mysql can use the merge algorithm for views like that
c) Columns added after the view-creation will be visible in the view immediately because of the select *
d) After the view creation, changes to the base table are not reaching the view
e) Views like these are actually insertable and updatable, as there is a 1:1 relation to the rows in the base table
f) As their name implies, views can’t be written to
[ Views (15%) - Creating Views ]
1. execute test;
2. deallocate prepare test;
3. prepare test from @a;
4. set @a:=concat('show create database ',ifnull(database(),'mysql'));
Which is the correct order?
a) 1,2,3,4 b) 4,3,2,1 c) 3,1,2,4 d) 4,3,1,2
Extra question: What does the prepared statement in the example actually do?
[ Prepared Statements (5%) - Using Prepared Statements from the mysql Client ]
1. mysqldump -p -u root --databases tt > tt.sql 2. mysqldump -p -u root tt > tt.sql
(Find all correct answers)
a) There is no difference b) The syntax of 1 is incorrect c) The syntax of 2 is incorrect d) 1 will include the create database statement e) 2 will include the create database statement
[ Importing and Exporting Data (5%) - Importing and Exporting Data from the Command Line ]
create table work1 (
site_id int,
sale_dt date,
amount decimal(10,2),
primary key (site_id,sale_dt));
create table work2 (
site_id int,
sale_dt date,
amount decimal(10,2),
primary key (site_id,sale_dt));
-- test data
create table wgen(a1 bit(1));
insert into wgen
values (1),(1),(1),(0),(1),(1),(1),(1),(0),(1);
insert ignore into work1
select rand()*100,
adddate('2010-01-01',
interval rand()*360 day),rand()*100000
from wgen v1,wgen v2,wgen v3,wgen v4;
insert ignore into work2
select rand()*100,adddate('2010-01-01',
interval rand()*360 day),rand()*100000
from wgen v1,wgen v2,wgen v3,wgen v4;
You would like to rewrite the query below in different ways.
select count(*)
from work1 v1
where not exists (
select * from work2
where site_id=v1.site_id and sale_dt=v1.sale_dt);
(Find all correct answers)
a) select count(*)
from work1 v1 left join work2 v2
on (v1.site_id=v2.site_id and v1.sale_dt=v2.sale_dt)
where v2.site_id is null and v2.sale_dt is null;
b) select count(*)
from work1 v1, work2 v2
where (v1.site_id<>v2.site_id and v1.sale_dt<>v2.sale_dt);
c) select count(*)
from work1 v1
where (v1.site_id,v1.sale_dt)
not in (select site_id,sale_dt from work2);
d) select count(*)
from (select * from work1 minus select * from work2) v1;
Extra question: How does the test-data generation work, and how many rows will it create?
[ Subqueries (10%) - Converting Subqueries to Joins ]
create table j1 (i int primary key); create table j2 like j1; insert into j1 values (1),(2),(3); insert into j2 values (1),(2),(3);
Queries:
a) select * from j1,j2; b) select * from j1,j2 where j1.i=j2.i; c) select * from j1,j2 where j1.i<>j2.i; d) select * from j1,j2 where j1.i>j2.i;
Results:
1) +---+---+ | i | i | +---+---+ | 1 | 1 | | 2 | 2 | | 3 | 3 | +---+---+ 3 rows in set (0.00 sec) 2) +---+---+ | i | i | +---+---+ | 2 | 1 | | 3 | 1 | | 1 | 2 | | 3 | 2 | | 1 | 3 | | 2 | 3 | +---+---+ 6 rows in set (0.00 sec) 3) +---+---+ | i | i | +---+---+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 1 | 2 | | 2 | 2 | | 3 | 2 | | 1 | 3 | | 2 | 3 | | 3 | 3 | +---+---+ 9 rows in set (0.00 sec) 4) +---+---+ | i | i | +---+---+ | 2 | 1 | | 3 | 1 | | 3 | 2 | +---+---+ 3 rows in set (0.00 sec)
[ Joins (10%) - Overview ]
create table jj1 (i int primary key); create table jj2 like jj1; insert into jj1 values (1),(2); insert into jj2 values (1),(3);
Queries:
a) select * from jj1 left join jj2 on (jj1.i=jj2.i); b) select * from jj1 right join jj2 on (jj1.i=jj2.i); c) select * from jj1 inner join jj2 (jj1.i=jj2.i);
Results:
1) +------+---+ | i | i | +------+---+ | 1 | 1 | | NULL | 3 | +------+---+ 2 rows in set (0.02 sec) 2) +---+---+ | i | i | +---+---+ | 1 | 1 | +---+---+ 1 row in set (0.00 sec) 3) +---+------+ | i | i | +---+------+ | 1 | 1 | | 2 | NULL | +---+------+ 2 rows in set (0.00 sec)
[ Joins (10%) - Overview ]
create table shop(
shop_id int primary key,
shop_name varchar(32));
insert into shop values
(1,'shop one'),
(2,'shop two'),
(3,'shop three');
create table product(
product_id int primary key,
product_name varchar(32),
product_type int);
insert into product values
(1,'product one',1),
(2,'product two',1),
(3,'product three',2);
create table sale(
shop_id int,
sale_dt date,
product_id int,
type int,
amount decimal(10,2),
key ix1(shop_id, sale_dt));
insert into sale values
(1,'2010-01-01',1,0,100.50),(1,'2010-01-01',2,0,100.50),
(3,'2010-02-01',3,1,100.50),(2,'2010-02-01',3,1,100.50);
select v2.shop_name,v1.sale_dt,v3.product_type,sum(v1.amount)
from sale v1, shop v2, product v3
where v1.shop_id=v2.shop_id and v1.product_id=v3.product_id
group by v1.shop_id,v1.sale_dt,v3.product_type
order by 1,2,3;
Possible results:
a) +------------+------------+--------------+----------------+ | shop_name | sale_dt | product_type | sum(v1.amount) | +------------+------------+--------------+----------------+ | shop one | 2010-01-01 | 1 | 201.00 | | shop two | 2010-02-01 | 2 | 100.50 | | shop three | 2010-02-01 | 2 | 100.50 | +------------+------------+--------------+----------------+ 3 rows in set (0.00 sec) b) +------------+------------+--------------+----------------+ | shop_name | sale_dt | product_type | sum(v1.amount) | +------------+------------+--------------+----------------+ | shop one | 2010-01-01 | 1 | 201.00 | | shop three | 2010-02-01 | 2 | 100.50 | | shop two | 2010-02-01 | 2 | 100.50 | +------------+------------+--------------+----------------+ 3 rows in set (0.00 sec) c) +------------+------------+--------------+----------------+ | shop_name | sale_dt | product_type | sum(v1.amount) | +------------+------------+--------------+----------------+ | shop one | 2010-01-01 | 1 | 201.00 | | shop three | 2010-02-01 | 1 | 100.50 | | shop two | 2010-02-01 | 2 | 100.50 | +------------+------------+--------------+----------------+ 3 rows in set (0.02 sec)
[ Querying for Data (15%) - Using SELECT to Retrieve Data ]