Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4991

Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4993

Warning: Cannot modify header information - headers already sent by (output started at /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php:4991) in /www/htdocs/v030397/mysql-qotd/wp-includes/feed-rss2.php on line 8
MySQL Question of the Day » urs http://mysql-qotd.casperia.net mysql 5.0/5.1 questions for learning purposes Fri, 06 Aug 2010 16:42:05 +0000 http://wordpress.org/?v=abc en hourly 1 Question 41: Which of the queries is the most efficient? http://mysql-qotd.casperia.net/archives/305 http://mysql-qotd.casperia.net/archives/305#comments Tue, 09 Mar 2010 19:29:57 +0000 urs http://mysql-qotd.casperia.net/?p=305  

-- 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 ]

]]>
http://mysql-qotd.casperia.net/archives/305/feed 1
Question 40: Which of the following queries return 0? http://mysql-qotd.casperia.net/archives/301 http://mysql-qotd.casperia.net/archives/301#comments Mon, 08 Mar 2010 18:27:18 +0000 urs http://mysql-qotd.casperia.net/?p=301  
(Find all correct answers)
 

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 ]
 

]]>
http://mysql-qotd.casperia.net/archives/301/feed 1
Question 39: Which of the alternatives below actually work? http://mysql-qotd.casperia.net/archives/295 http://mysql-qotd.casperia.net/archives/295#comments Sun, 07 Mar 2010 15:42:36 +0000 urs http://mysql-qotd.casperia.net/?p=295  
Imagine you have to create a running number for a query result and select only the line 1, 2 and 18.
You choose user variables as a means to do that.
 
(Find all correct answers)
 

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 ]
 

]]>
http://mysql-qotd.casperia.net/archives/295/feed 1
Question 38: Which of the following statements are true? http://mysql-qotd.casperia.net/archives/279 http://mysql-qotd.casperia.net/archives/279#comments Sat, 06 Mar 2010 16:45:19 +0000 urs http://mysql-qotd.casperia.net/?p=279  
Imagine, that in your environment there is the policy, that the reporting tool doesn’t use your tables directly,
but that all access is done via views. So you create views for all your tables like this:
 

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 ]
 

]]>
http://mysql-qotd.casperia.net/archives/279/feed 1
Question 37: Which is the correct order of the steps below to execute a prepared statement from the mysql client? http://mysql-qotd.casperia.net/archives/276 http://mysql-qotd.casperia.net/archives/276#comments Fri, 05 Mar 2010 16:20:24 +0000 urs http://mysql-qotd.casperia.net/?p=276  

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 ]
 

]]>
http://mysql-qotd.casperia.net/archives/276/feed 1
Question 36: What is the difference between the following export operations? http://mysql-qotd.casperia.net/archives/273 http://mysql-qotd.casperia.net/archives/273#comments Thu, 04 Mar 2010 17:30:09 +0000 urs http://mysql-qotd.casperia.net/?p=273 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%) - [...]]]>  

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 ]
 

]]>
http://mysql-qotd.casperia.net/archives/273/feed 1
Question 35: Which of the following alternatives return the same result as the original? http://mysql-qotd.casperia.net/archives/268 http://mysql-qotd.casperia.net/archives/268#comments Wed, 03 Mar 2010 15:44:09 +0000 urs http://mysql-qotd.casperia.net/?p=268  

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 ]
 

]]>
http://mysql-qotd.casperia.net/archives/268/feed 1
Question 34: Connect the queries a-d to the resultsets 1-4 http://mysql-qotd.casperia.net/archives/265 http://mysql-qotd.casperia.net/archives/265#comments Tue, 02 Mar 2010 16:42:16 +0000 urs http://mysql-qotd.casperia.net/?p=265 j2.i;   Results:   1) +---+---+ | i | i | +---+---+ | 1 | 1 | | 2 | 2 | | 3 | [...]]]>  

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 ]
 

]]>
http://mysql-qotd.casperia.net/archives/265/feed 1
Question 33: Connect the queries a-c to the resultsets 1-3 http://mysql-qotd.casperia.net/archives/262 http://mysql-qotd.casperia.net/archives/262#comments Mon, 01 Mar 2010 16:35:02 +0000 urs http://mysql-qotd.casperia.net/?p=262  

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 ]
 

]]>
http://mysql-qotd.casperia.net/archives/262/feed 1
Question 32: What is the result of the following query? http://mysql-qotd.casperia.net/archives/258 http://mysql-qotd.casperia.net/archives/258#comments Sun, 28 Feb 2010 06:58:57 +0000 urs http://mysql-qotd.casperia.net/?p=258  

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 ]
 

]]>
http://mysql-qotd.casperia.net/archives/258/feed 1