JSFiddle - React, Tailwind, and code Playground

by ysuper

HTML

<script src="//cdn.jsdelivr.net/alasql/latest/alasql.min.js"></script>
<h1>Alasql</h1>
<a href="https://github.com/agershun/alasql/wiki/Examples" target="_blank">Examples</a>
<br><br>
Query result: <div id="result"></div>
<br>
Query result1: <div id="result1"></div>
<br>
Query result2: <div id="result2"></div>

JavaScript

// Fill table with data
var person = [ 
    { name: 'bill' , sex:'M', income:50000 },
    { name: 'sara' , sex:'F', income:100000 },
    { name: 'larry' , sex:'M', income:90000 },
    { name: 'olga' , sex:'F', income:85000 },
];

// Do the query
var res = alasql("SELECT * FROM ? person WHERE sex='F' AND income > 60000", [person]);

document.getElementById("result").innerHTML = JSON.stringify(res);

//////////////////////////////////////////////////////////////////////////////////////////

// Create database
var db = new alasql.Database();

// Create table with column definitions
db.exec('CREATE TABLE person (name STRING, sex STRING, income INT)');

// Fill table with data
db.tables.person.data = [ { name: 'bill' , sex:'M', income:50000 },
                    { name: 'sara' , sex:'F', income:100000 }];

// Do the query
document.getElementById("result1").innerHTML = JSON.stringify(db.exec("SELECT * FROM person WHERE sex='F' AND income > 60000"));

//////////////////////////////////////////////////////////////////////////////////////////

// Create database
var db = new alasql.Database();
db.exec('CREATE TABLE students (studentID INT, school STRING, City STRING)');

db.tables.students.data = [
       {studentID: 55, school: 'abc', City:'abc'},
       {studentID: 56, school: 'klm', City:'xyz'},
       {studentID: 57, school: 'nyz', City:'xyz'}
    ];
    
var res = db.exec("SELECT * FROM students "
  +" WHERE (studentID = 55 AND school='abc') AND (City='xyz' OR City ='abc')");

document.getElementById("result2").innerHTML = JSON.stringify(res);