Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Info

Keywords are not case-sensitive. In this document, keywords such as SELECT are capitalized for illustration purposes.

Query Syntax

ExcelSQL queries are SELECT statements of below form:

...

Code Block
SELECT [NO_HEADER_ROW] [DISTINCT] * | column1 [AS alias1], column2 [AS alias2], ...
   [FROM worksheet]
   [WHERE condition]
   [GROUP BY column1, column2, ...]
   [HAVING condition]
   [ORDER BY column1 [DESC|ASC], column2 [DESC|ASC], ...]
   [LIMIT number [OFFSET number]
GO

SELECT clause

The SELECT clause is used to select data from an Excel worksheet.

...

Code Block
SELECT * FROM Sheet1
GO
SELECT city, zipcode FROM zips
GO
SELECT COUNT(*) FROM zips
GO
SELECT SUM(population) AS [Total Population] FROM zips
GO

Back to top

NO_HEADER_ROW option

NO_HEADER_ROW

By default, the first row is considered as headers. If there is no header, specify the NO_HEADER_ROW option and Excel's headers will be used as column names.

...

Code Block
SELECT NO_HEADER_ROW * FROM Sheet2
GO
SELECT NO_HEADER_ROW COUNT(*) FROM Sheet2
GO
SELECT NO_HEADER_ROW B, C FROM Sheet2
GO
SELECT NO_HEADER_ROW [employee].A, [project].B AS Project FROM employee INNER JOIN project ON [employee].B = [project].B
GO

DISTINCT clause 

DISTINCT

A column can contain duplicate values, and to list the distinct values, use the SELECT DISTINCT clause. The DISTINCT clause can be used to return only distinct values from a set of records.

...

Code Block
SELECT DISTINCT city FROM zips
GO
SELECT DISTINCT * FROM employee
GO

FROM clause

Determines the specific dataset to examine to retrieve data. For Excel, this would be indicated as a specific worksheet.

...

where the Excel file name is workbook and the worksheet name is worksheet.

SubTable feature

In addition, to query a range of cells in the worksheet, use the following syntax to specify the starting cell and the ending cell of the range.

...

Code Block
SELECT NO_HEADER_ROW B, C, D FROM orders LIMIT 1000 OFFSET 5
GO

Back to top

WHERE clause 

The WHERE clause, sometimes called the predicate, states the qualifying conditions for a query. You can combine the ExcelSQL Arithmetic Operators and the ExcelSQL Comparison Operators in the WHERE clause. Multiple conditions can be joined by the AND and OR clauses, optionally surrounded by (parentheses) to group them. Only the records that satisfy the specified criteria are returned by the query.

...

Code Block
SELECT * FROM mixedDataTypes.Sheet1
WHERE TO_CHAR(column2) = '11.0'
GO
SELECT * FROM mixedDataTypes.Sheet1
WHERE TO_CHAR(column2) = '11.0' OR TO_CHAR(column2) = 'test'
GO

GROUP BY clause

The GROUP BY clause is used in combination with aggregate functions to group the results by one or more columns. See the SQL Functions page for a list of aggregate functions supported by ExcelSQL.

...

Code Block
SELECT name, COUNT(orders) FROM zips GROUP BY name
GO

Back to top

HAVING clause

The HAVING clause states the qualifying conditions for aggregated values. It is used in conjunction with aggregate functions to filter aggregated values.

...

Code Block
SELECT name, COUNT(orders) FROM zips GROUP BY name HAVING COUNT(orders) > 25
GO

ORDER BY clause

ORDER BY is used to sort the results by one or more columns and sorts in ascending order by default. To sort in descending order, use the DESC keyword.

...

Code Block
SELECT PostalCode FROM zips ORDER BY TO_CHAR(PostalCode)
GO

LIMIT and OFFSET clause

LIMIT and OFFSET allow you to retrieve just a portion of the result set returned by the query. LIMIT restricts the number of rows returned by the SELECT statement. OFFSET indicates how many rows to skip. OFFSET 0 is the same as omitting the OFFSET clause.

...

Code Block
SELECT * FROM zips LIMIT 5
GO
SELECT * FROM city LIMIT 2 OFFSET 3

Set Operators

ExcelSQL supports the below set operators. Set operators combine results from two or more queries into a single result set.

MINUS, EXCEPT, INTERSECT

UNION and UNION ALL

MINUS

The MINUS operator returns only unique rows that are returned by the first query but not by the second query.

...

Code Block
SELECT city, zipcode FROM zips
MINUS
SELECT city, zipcode FROM pins

Back to top

EXCEPT 

The EXCEPT operator returns rows that are returned by the first query but not by the second query.

...

Code Block
 SELECT city, zipcode FROM zips
 EXCEPT
 SELECT city, zipcode FROM pins 

INTERSECT 

The INTERSECT operator returns only rows that are returned by both queries.

...

Code Block
SELECT city, zipcode FROM zips
 INTERSECT
 SELECT city, zipcode FROM pins

UNION Operator

The UNION operator combines the results of two queries and eliminates duplicate rows.

...

Code Block
SELECT city, zipcode FROM zips 
UNION
SELECT city, zipcode FROM pins
GO

UNION ALL Operator

The UNION ALL operator combines the results of two queries. It does not remove duplicate rows. All rows are returned.

...

Code Block
SELECT city, zipcode FROM zips 
UNION ALL
SELECT city, zipcode FROM pins
GO

SUBQUERY 

A subquery is a SELECT statement nested inside a SELECT statement or inside another subquery. It is usually added within the WHERE clause of another ExcelSQL SELECT statement. 

...

Code Block
SELECT (SELECT P_id FROM Project where E_id=1), Fname FROM Employee WHERE E_id=1
GO
SELECT a,b AS total_sum FROM (SELECT SUM(expenses) AS a, SUM(Salary) AS b FROM Employee)
GO
SELECT * FROM Employee E WHERE E_id IN (SELECT P_id FROM Project WHERE P_id = 10001)
GO

Back to top

SQL Aliases

SQL aliases are used to temporarily assign a different name to a table or column heading. Basically aliases are created to make column names more readable. The WHERE, ORDER BY, GROUP BY, HAVING and JOIN clauses also support aliases for ExcelSQL.

...

Code Block
SELECT zipc AS ZipCode FROM zips
GO
SELECT city FROM zips AS Z
GO
SELECT A.E_id FROM employee AS A WHERE A.E_id = 1
GO
SELECT COUNT(A.E_id) as B FROM Employee AS A HAVING COUNT(A.age) < 10
GO
SELECT A.E_id, B.P_id FROM Employee AS A INNER JOIN Project AS B ON B.p_id = A.e_id
GO

Joins 

There are different types of joins available in ExcelSQL: INNER JOIN, LEFT JOIN and RIGHT JOIN. INNER JOIN returns rows when there is a match on both tables. LEFT JOIN returns all rows from the left table even if there are no matches in the right table. RIGHT JOIN returns all rows from the right table even if there are no matches in the left table. You can find the syntax for the different joins below.

Back to top

INNER JOIN clause

SELECT column1, column2 FROM worksheet1 INNER JOIN worksheet2 ON worksheet1.column1 = worksheet2.column2

...

Code Block
SELECT C.name, O.customerID from [pubs].Customers C
INNER JOIN [bi].Orders O ON C.customerID = O.customerID
INNER JOIN [pubs].Sales S ON S.orderID > C.customerID

LEFT JOIN clause

SELECT column1, column2 FROM worksheet1 LEFT JOIN worksheet2 ON worksheet1.column1 = worksheet2.column2

...

Code Block
SELECT C.name, O.customerID FROM Customers C LEFT JOIN Orders O ON C.customerID = O.customerID

Back to top

RIGHT JOIN clause

SELECT column1, column2 FROM worksheet1 RIGHT JOIN worksheet2 ON worksheet1.column1 = worksheet2.column2

...

Code Block
SELECT C.name, O.customerID FROM Customers C LEFT JOIN Orders O ON C.customerID = O.CustomerID
UNION ALL
SELECT C.name, O.customerID FROM Customers C RIGHT JOIN Orders O ON C.customerID = O.customerID

Back to top

Self Join

SELECT Column1, Column2 FROM Worksheet1 A, Worksheet1 B  WHERE  A.Column1 = B.Column1

...

Code Block
SELECT A.E_id, B.E_id FROM employee A, employee B WHERE A.E_id = B.Mgr_id

CROSS JOIN clause

SELECT Column1,Column2 FROM Worksheet1 CROSS JOIN Worksheet2

...

Code Block
SELECT A.E_id, B.P_id, A.fname FROM employee A CROSS JOIN project B

Multiple AND/OR Conditions on Joins

Example

Code Block
SELECT * FROM employee INNER JOIN project ON employee.E_id = Project.P_id AND employee.fname = project.p_name AND employee.salary = project.p_profit
GO
SELECT * FROM employee LEFT JOIN project ON employee.E_id = Project.P_id OR employee.fname = project.p_name OR employee.salary = project.p_profit

Derived Tables

The Derived table is a technique for creating a temporary set of records which can be used within another query in ExcelSQL. You can use derived tables to shorten long SQL queries.

...

Code Block
 SELECT Project.P_id, Project.budget FROM 
        (SELECT E.E_id, E.Salary FROM Employee E) emp 
                INNER JOIN Project on emp.E_id = Project.P_id 

Back to top

ExcelSQL Arithmetic Operators 

Arithmetic operators perform mathematical operations on two expressions of one or more datatypes of the integer data type category.

...

Code Block
SELECT 15+10-5*5/5 FROM [bi].zips
GO
SELECT cust_name, openg_amt, receiv_amt, outstand_amt, (receiv_amt*5/ 100) AS commission  
FROM [bi].customer  
WHERE outstanding_amt <= 4000
GO
SELECT StockUnits, StockUnits%3 AS "Modulo by 3"
FROM [bi].products

ExcelSQL Comparison Operators 

Comparison operators test whether two expressions are the same.The following table lists the ExcelSQL comparison operators.

...

Code Block
SELECT * FROM [bi].zips
WHERE city = 'Cochin'
GO
SELECT * FROM [bi].zips
WHERE city <> 'Cochin'
GO
SELECT *
FROM  [bi].zips
WHERE zipcode <= 682017
GO

Back to top

Configuration Functions

SELECT @@database

This function returns information on the current database context.

...

Code Block
SELECT @@schema

Utility Statements

DATABASE database_name

This statement sets the database context and keeps this context for subsequent statements until the end of the session or another DATABASE statement is encountered.

...