Write Sql query to select all username who had nokia handsets "N72" in 2008

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rahullko05
    New Member
    • Oct 2008
    • 34

    Write Sql query to select all username who had nokia handsets "N72" in 2008

    Write a my sql query to select all username who had nokia purchased handsets "N72" in the year 2008.

    tables given below:

    Thanks

    Code:
    -- phpMyAdmin SQL Dump
    -- version 3.2.0.1
    -- http://www.phpmyadmin.net
    --
    -- Host: localhost
    -- Generation Time: Nov 02, 2010 at 10:51 AM
    -- Server version: 5.1.37
    -- PHP Version: 5.3.0
    
    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
    
    --
    -- Database: `intTest`
    --
    
    -- --------------------------------------------------------
    
    --
    -- Table structure for table `handsets`
    --
    
    CREATE TABLE IF NOT EXISTS `handsets` (
      `handsets_id` varchar(10) NOT NULL,
      `user_id` varchar(10) NOT NULL,
      `handsets` varchar(20) NOT NULL,
      `purchase_date` varchar(20) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    
    --
    -- Dumping data for table `handsets`
    --
    
    INSERT INTO `handsets` (`handsets_id`, `user_id`, `handsets`, `purchase_date`) VALUES
    ('1', '1', 'n72', '2008-03-19'),
    ('2', '1', '1610', '2009-03-19'),
    ('3', '1', '3210', '2009-03-19'),
    ('4', '2', 'n72', '2008-03-19'),
    ('5', '3', '6680', '2009-03-19'),
    ('6', '3', '6681', '2009-03-19'),
    ('7', '3', 'n72', '2009-03-19');
    
    -- --------------------------------------------------------
    
    --
    -- Table structure for table `users`
    --
    
    CREATE TABLE IF NOT EXISTS `users` (
      `user_id` varchar(10) NOT NULL,
      `username` varchar(30) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    
    --
    -- Dumping data for table `users`
    --
    
    INSERT INTO `users` (`user_id`, `username`) VALUES
    ('1', 'nipun'),
    ('2', 'rohit'),
    ('3', 'sumit');
  • Abubakr

    #2
    First the tables:

    Code:
    -- Table structure for table `users`
    --
    
    CREATE TABLE `users` (
      `user_id` int(11) NOT NULL auto_increment,
      `username` varchar(30) NOT NULL,
      PRIMARY KEY  (`user_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
    
    
    -- Table structure for table `handsets`
    --
    
    CREATE TABLE `handsets` (
      `handsets_id` int(11) NOT NULL auto_increment,
      `user_id` int(11) NOT NULL,
      `handsets` varchar(20) NOT NULL,
      `purchase_date` date NOT NULL,
      PRIMARY KEY  (`handsets_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
    Second the query:

    Code:
    SELECT username FROM users ,handsets
    WHERE users.user_id=handsets.user_id
    and handsets='n72'
    and purchase_date between '2008-01-01' and  '2008-12-31'
    Last edited by MMcCarthy; Nov 2 '10, 11:43 AM. Reason: added code tags

    Comment

    • rahullko05
      New Member
      • Oct 2008
      • 34

      #3
      Thanks a lot...it works

      Comment

      Working...