How to Write SQL Queries & Work With XML & XSLT

Database Development, Web Development

SQL Language Logo

SQL

Vocabulary

SQL SELECT Statement Characteristics

Before you start writing queries, it's a good idea to get familiar with the data you are working with first. If you are able, go in and explore the databases, tables, and fields you have to work with. If not, there are some queries to get you in the know (for MySQL). Type in the query:

SHOW DATABASES

to get a list of databases. Select a database to work with and use the query:

SHOW TABLES in DatabaseOne

to get a list of all the columns you have to work with in that table.

SELECT *
FROM employees;

or

SELECT EmployeeID, FirstName, HireDate, City
FROM Employees;
SELECT *
FROM employees
WHERE City = 'Los Angeles';

Use this to get opposite results:

SELECT *
FROM employees
WHERE City <> 'Los Angeles';
SELECT *
FROM employees
WHERE City = 'London' OR City = 'Seattle';

Use this for 2+ cities:

SELECT *
FROM employees
WHERE City IN ('Seattle', 'Tacoma', 'Redmond');
SELECT *
FROM employees
WHERE HireDate BETWEEN '1-june-1992' AND '15-december-1993';

Use this to get opposite results:

SELECT *
FROM employees
NOT BETWEEN '1-june-1992' AND '15-december-1993';
SELECT *
FROM employees
ORDER BY City
SELECT *
FROM employees
ORDER BY City
LIMIT 100;
SELECT first_name, last_name
FROM people_city
WHERE address_city = "Los Angeles" AND birth_date BETWEEN '1991-11-01'
ORDER BY last_name
LIMIT 100;
SELECT first_name, last_name
FROM people_city
WHERE address_city = "Los Angeles" AND birth_date >= (DATE_SUB(CURDATE(),INTERVAL 30))
ORDER BY last_name
LIMIT 100;
SELECT birthdate_city.first_name, birthdate_city.last_name
FROM birthdate_city
JOIN haircolor_city USING (user_id)
WHERE hair_color = "red" AND birth_date BETWEEN '1991-11-01' AND '1993-11-01'
ORDER BY last_name;
SELECT birthdate_city.first_name, birthdate_city.last_name
FROM birthdate_city
JOIN haircolor_city USING (user_id)
WHERE hair_color IS NULL;
SELECT *
FROM customers
WHERE Country IN ('Poland', 'Canada', 'Ireland');
SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Customers AS c, Orders AS o
WHERE c.CustomerName = "Billy" AND c.CustomerID = o.CustomerID;

———

https://technet.microsoft.com/en-us/library/bb264565(v=sql.90).aspx
https://blog.hubspot.com/marketing/sql-tutorial-introduction
https://www.w3schools.com/sql/sql_select.asp

Retrieve XML Data using SQL

By storing an XML document within a SQL Server database, you're able to create queries that retrieve both relational data and specific information within the XML instance related to that data.

The easiest way to retrieve an individual value from an XML instance is to use the value() method. You call it by first specifying the name of the xml object that contains the target data and then providing two arguments: an XQuery expression and the data type of the returned value. XQuery is a scripting language used to access XML data. Here's an example of the value() method:

xml_object.value('xquery_exp', 'data_type')

Here's an example with an XML file of cars and then the SQL SELECT statement to return a list of all cars using the .nodes method:

<?xml version="1.0" encoding="UTF-8"?>
<Cars>
  <Car id="1234">
    <Make>Volkswagen</Make>
    <Model>Eurovan</Model>
    <Year>2003</Year>
    <Color>White</Color>
  </Car>
  <Car id="5678">
    <Make>Honda</Make>
    <Model>CR-V</Model>
    <Year>2009</Year>
    <Color>Black</Color>
    <Mileage>35,600</Mileage>
  </Car>
</Cars>
SELECT t.value('Make[1]','NVARCHAR(MAX)')+' '+t.value('Model[1]','NVARCHAR(MAX)') AS CarType FROM @cars.nodes('/Cars/Car') x(t);

Result:

Volkswagen Eurovan
Honda CR-V

———

https://docs.microsoft.com/en-us/sql/relational-databases/xml/retrieve-and-query-xml-data
https://stackoverflow.com/questions/4855476/extract-a-value-from-sql-xml-field
http://control.cyriouswiki.com/How-To+Write+SQL+to+Extract+an+XML+Value+from+a+Database+Field
https://www.red-gate.com/simple-talk/sql/learn-sql-server/sql-server-xml-questions-you-were-too-shy-to-ask/

Transforming XML Data Into Another Format Using XSLT

The standard way for you to transform XML data into another format is with Extensible Stylesheet Language Transformations (XSLT). You can convert XML documents into HTML, plain text, or a different XML schema using the built-in XSLTRANSFORM function. You can easily build an HTML page outputting the data from an XML file using XSLT.

———

https://www.ibm.com/support/knowledgecenter/en/SSEPGG_10.5.0/com.ibm.db2.luw.xml.doc/doc/c0050648.html