sql-sqlite - WordPress.com

advertisement
SQL Queries
Below are the statements to execute for a set of queries on top of the database we
have created. For each query, click on “Execute SQL”, paste the query in the “Enter
SQL” text area and click on “Run SQL”.
For Example1:
Query 1: Select everything from «quotations»
SELECT *
FROM quotations
Query 2: Select all Short Titles
SELECT ShortTitle
FROM texts
Query 3: Select texts whose Short Title is “Sat”
SELECT *
FROM texts
WHERE ShortTitle = ‘Sat’
Query 4: (Join operation) Select all Declarations whose topic is Hell
SELECT Q.Declaration
FROM quotation Q, topics O
WHERE Q.Topic = O.ID
AND O.Topic = ‘Hell’
Query 5: Select all texts ordered by CameronNum
SELECT *
FROM texts
ORDER BY CameronNum
Query 6: How many quotations do we have ?
SELECT COUNT(*)
FROM quotations
L Change Query 4 to have
SELECT COUNT(O.Declaration)
What does it do now ?
For Example2:
Query 1: Give me the list of DISTINCT Sellers
SELECT DISTINCT Sellers
FROM Schoenberg
 Remove the keyword DISTINCT and see the difference in the results
Query 2: Give me the Places whose manuscripts have more than 300 Folios
SELECT DISTINCT Place
FROM Schoenberg
WHERE Folios > 300
 Notice the values “Italy”, “Italy,Naples”, “Italy, central, Fossombrono”. How do we
find manuscripts in just “Italy” ? With the following query
Query 3: Give the manuscripts whose recorded place includes “Italy”
SELECT *
FROM Schoenberg
WHERE Place LIKE “Italy%”
Query 4: What is the total number of recorded folios ?
SELECT sum(Folios)
FROM Schoenberg
Query 5: What is the average number of recorded folios ?
SELECT avg(Folios)
FROM Schoenberg
Query 6: Select the Manuscript IDs ordered by the number of Lines
SELECT Manuscript_ID, Lines
FROM Schoenberg
ORDER BY Lines
 By default increasing . Change to “…ORDER BY Lines DESC” and see !
 Notice the empty Lines at the beginning. To remove them, change the query to
SELECT Manuscript_ID, Lines
FROM Schoenberg
WHERE Lines > 0
ORDER BY Lines
Query 7: Give the number of manuscripts (sum) per Place
SELECT Place, count(*)
FROM Schoenberg
GROUP BY Place
Download