Definition of SQL

SQL and Relational database tables

DDL and DML

Simple SQL queries useing the Select statement

The SELECT statement

In SQL, querying data is performed by a SELECT statement. A select statement has 6 key components;

library(DBI)
SELECT colnames
FROM tablename
GROUP BY colnames
WHERE conditions
HAVING conditions
ORDER BY colnames
Select * 
FROM SN7577;
Select web1, web2, web3, web4
From SN7577;
Select w*
From SN7577;
Select *
From SN7577
Limit 10;

Exercise

Write a query which returns the first 5 rows from the SN7577 table with only the columns Q1,Q2,Q3,Q4 and numage.

Solution:

 Select Q1, Q2, Q3, Q4, numage
 From SN7577
 Limit 5;

The WHERE Clause

Select  Q1,  Q3, Q4
From SN7577
Where Q1 = 2;

Using more complex logical expressions in the WHERE clause

  • We can also use the AND and OR keywords to build more complex selection criteria.
Select  Q1,  Q3, Q4
From SN7577
Where Q1 > 5 and Q3 <> 2;
  • You can ensure the precedence of the operators by using brackets.
  • Note: Judicious use of brackets can also aid readability
Select  Q1,  Q3, Q4
From SN7577
Where Q1 = 5 or (Q3 <> 2 and Q4 > 8);
  • The following query returns the rows where the value of Q1 either 5,6,7 or 8
Select  Q1,  Q3, Q4
From SN7577
Where Q1 > 5 and Q1 < 8;
  • The same results could be obtained by using the BETWEEN or IN operators
Select  Q1,  Q3, Q4
From SN7577
Where Q1 Between 5 and 8;
  • using In
Select  Q1,  Q3, Q4
From SN7577
Where Q1 In (5, 6, 7, 8);
  • The list of values in brackets do not have to be contiguous or even in order.

Exercise 1

In the SN7577 table the values of the web1, web2, web3, web4 columns are all -1, 0 or 1. The numage columns can be any value between 0 and 95.

Write a query which returns the web1, web2, web3, web4 and numage columns from the SN7577 table. The web1, web2, web3, web4 values should all be either 0 or 1 and the numage values should be between 30 and 40 inclusive. There are many ways of doing this, but try to use two different inequalities, an IN clause and a BETWEEN clause.

Solution:

select web1, web2, web3 ,  numage
from sn7577
where web1 >= 0 and web2 in (0, 1) and web3 <> -1 and numage between 30 and 40;

Sorting resutls

Select Q1,  Q3, Q4
From SN7577
Where Q1 In (5, 6, 7, 8)
Order By Q1;
Select  Q1,  Q3, Q4
From SN7577
Where Q1 In (5, 6, 7, 8)
Order By Q1 Desc;
Select  Q1,  Q3, Q4
From SN7577
Where Q1 In (5, 6, 7, 8)
Order By Q1 Desc, Q3 Asc;
Key points
  • Strictly speaking SQL is a standard, not a particular implementation

  • SQL implementation are sufficiently close that you only have to learn SQL once

  • The DDL constructs are used to create tables and other database objects

  • The DML constructs, typically the SELECT statement is used to retrieve data from one or more tables

  • The SELECT statement allows you to slice and dice the columns and rows of the dataset so that the query only returns the data of interest

LS0tCnRpdGxlOiAiVGhlIFNlbGVjdCBTdGF0ZW1lbnQiCm91dHB1dDogaHRtbF9ub3RlYm9vawotLS0KCiMgRGVmaW5pdGlvbiBvZiBTUUwKKiBTUUwgb3IgU3RydWN0dXJlZCBRdWVyeSBMYW5ndWFnZSBpcyBhbiBpbnRlcm5hdGluYWwgc3RhbmRhcmQgZm9yIG1hbmlwdWxhdGluZyBkYXRhIGluIGEgcmVsYXRpb25hbCBkYXRhYmFzZS4gCgoqIEVhY2ggUmVsYXRpb25hbCBEYXRhYmFzZSBzeXN0ZW0gbGlrZSBPcmFjbGUsIE15U1FMIG9yIFNRTGl0ZSBpbXBsZW1lbnRzIGl0cyBvd24gdmFyaWF0aW9uIG9mIHRoZSBzdGFuZGFyZC4KCiogRm9ydHVuYXRlbGV5IGZvciB0aGUgdHlwZXMgb2YgY29tbWFuZHMgYW5kIHF1ZXJpZXMgdGhhdCB3ZSB3aWxsIHdhbnQgdG8gd3JpdGUsIGFsbCBvZiB0aGUgaW1wbGVtZW50YXRpb25zIGFyZSBtdWNoIGluIGFncmVlbWVudC4gCgoqIFRoZSByZWxhdGl2ZWx5IHN0cmFpZ2h0Zm9yd2FyZCBTZWxlY3QgcXVlcmllcyB3ZSB3aWxsIGJlIHdyaXRpbmcgdG8gYWNjZXNzIGRhdGEgaW4gb3VyIFNRTGl0ZSBkYXRhYmFzZSB3aWxsIGV4ZWN1dGUgdW4tYWx0ZXJl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