Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Converting SQL script to JavaScript?

    so I will be undergoing a programming test tomorrow for a project interview in my company.

    Beyond doing a basic training in JavaScript, the content of which should be similar to the JS Basics in w3schools, I havent really worked purely in this langauage. Instead I have done a fair bit of ExtJS, java and a bit of jQuery too.

    My interviewers are aware of this and have given me a task to perform in JavaScript tomorrow for me to prepare for.

    They've told me to: "Convert SQL script to JavaScript". That's all. They will provide the SQL script and I have to convert it to JavaScript. No PHP/Ajax/server side language.

    Can you please give me some relevant information or website that can point me in the right direction? Thank you for looking into this!

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Good luck with this one

    Javascript (as a client/browser language) cannot connect to a SQL database, therefore "translating" SQL script into Javascript is impossible by all means.

    I'm not aware about server side Javascript implementations like Node.js ... it might be possible to connect to SQL servers there. But you said "no server side language", hence no translation :-) Even on server side you won't translate SQL to Javascript, you would create a SQL query string in Javascript ...

    Anyone: Please correct me if I'm wrong, but this sounds like an interview "hoax"

  • #3
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Agreed. This sounds like non-sense to me. Although: though node.js is usually referred to as "server-side Javascript" it isn't server-side per se. It's just a packaged Javascript engine with a core library, simply put. You can run it perfectly fine without a server, it's usually just somewhat pointless.

    Unless you mentioned you are experienced in Node.js, I'm pretty sure they weren't talking about Node.js if they want to test you in basics of Javascript.

    the content of which should be similar to the JS Basics in w3schools
    Don't use w3schools

    Instead I have done a fair bit of ExtJS, java and a bit of jQuery too.
    I'm not sure why you put Java right in between two major Javascript frameworks. Since you have worked "a fair bit" with all of those I hope you're aware that Java and Javascript are very different languages (though you might not know, since you don't know any vanilla Javascript [didn't you ever feel the urge to learn something about the language you are using?])

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,376
    Thanks
    11
    Thanked 592 Times in 572 Posts
    they might have an array of objects or other object-store items and want you to code custom reports from the data in a way related to how SQL can make reports from a database.

    there are a few SQL-ish engine for js out right now:
    http://www.sequelsphere.com/
    http://www.thomasfrank.se/SQLike/?js...amp;posts2=300

    are the most well-known AFAIK.

    you can also use WebSQL on mobile and you can even run a full sqlLite instance in pure javascript: https://github.com/kripken/sql.js
    Last edited by rnd me; 11-21-2013 at 07:50 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,027
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    I think I can make some sort of sense of this.

    Given: a table of automobile makes (makeid, makename) and a table of automobile models (makeid, modelid, modelname).
    Given: the SQL query
    Code:
            SELECT makename, modelname
            FROM makes INNER JOIN models ON makes.makeid = models.makeid
            ORDER BY makename, modelname
    Requirement: Emulate that SQL query and those tables using JavaScript objects and JS code.
    I think the point of this exercise might be to find out if the applicant understands how SQL works, in the sense that (in theory, though of course not in practice, for performance reasons) it does a full cartesian product and then applies the various flters (WHERE and ON conditions) to that product. Too many people just think that SQL queries are some kind of magic, without understanding how the basics work, under the covers.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,027
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    So something like this:
    Code:
    function Make( id, name ) 
    {
        this.makeid = id;
        this.makename = name;
    }
    function Model( make, id, name )
    {
        this.makeid = make;
        this.modelid = id;
        this.modelname = name;
    }
    var makes = [
        new Make( 1, "Ford" ),
        new Make( 2, "Toyota")
    ];
    var models = [
        new Model( 1, 1, "Escape" ),
        new Model( 1, 2, "Escort" ),
        new Model( 2, 3, "Camry" ),
        new Model( 2, 4, "Corolla" )
    ];
    
    functon MakeModel( make, model )
    {
        this.makeid = make.makeid;
        this.makename = make.makename;
        this.modelmakeid = model.makeid;
        this.modelid = model.modelid;
        this.modelname = model.modelname;
    }
    
    function query( )
    {
        var cartesian = [ ];
        for ( var mk = 0; mk < makes.length; ++mk )
        {
            for ( var md = 0; md < models.length; ++md )
            {
                cartesian.push( new MakeModel( makes[mk], models[md] ) );
            }
        }
        // so at this point we have every possible make joined with every possible model...
        // we have the full cartesian product
        // so we need to apply the filter...
        // our only filter is ON make.makeid = model.makeid
        var result = [ ];
        for ( var c = 0; c < cartesian.length; ++c )
        {
            var test = cartesian[c];
            if ( test.makeid == test.modelmakeid ) { result.push( test ); }
        }
        // filter applied...
    
        // code not shown but should be obvious:
        // -- sort the result array by makename, modelname
        // -- return only the requested fields for result array: makename, modelname
    }
    Is it efficient? Of course not. And no SQL engine except the most primitive would actually work in this way. But it does demonstrate that the user understands the theory behind SQL queries.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •