2022-08-05 20:30:04 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								---
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 11:58:34 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								categories:
							 
						 
					
						
							
								
									
										
										
										
											2022-08-05 20:30:04 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  -  Databases
							 
						 
					
						
							
								
									
										
										
										
											2022-08-21 11:00:04 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  -  Programming Languages
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 11:58:34 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								tags: [SQL]
							 
						 
					
						
							
								
									
										
										
										
											2022-08-05 20:30:04 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								# SQL: Language structure
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								Before we start using the syntax we need to understand the grammar:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 11:58:34 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Expressions differ from clauses and predicates in that they are not the mechanism for returning data (i.e. declaring a clause and a logical colllllndition) they do something to the data, as part of the retrieval. This is a bit subtle:
							 
						 
					
						
							
								
									
										
										
										
											2022-08-05 20:30:04 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 11:58:34 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								-  `SELECT name FROM model WHERE cores = "4"` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  -  This retrieves the models that have 4 cores
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-  `SELECT count(*) FROM model WHERE cores = "4" ` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  -  This counts the number of models that are returned where the counting is a function over and above the retrieval itself.
							 
						 
					
						
							
								
									
										
										
										
											2022-08-05 20:30:04 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								### Examples from `computer_sales.db`
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								`sqlite> SELECT * from model WHERE cpu_speed=0.7`  : return all models with a CPU speed equal to 0.7:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 11:58:34 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2022-08-05 20:30:04 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								model_id    manufacturer_id  name                    cpu_speed   ram         cores       wifi        release_date
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----------  ---------------  ----------------------  ----------  ----------  ----------  ----------  ------------
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								1           1                Raspberry Pi 1 Model A  0.7         256.0       1           0           2013-02-01
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								2           1                Raspberry Pi 1 Model B  0.7         256.0       1           0           2012-04-01
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								3           1                Raspberry Pi 1 Model B  0.7         512.0       1           0           2012-10-01
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								4           1                Raspberry Pi 1 Model A  0.7         512.0       1           0           2014-11-01
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								5           1                Raspberry Pi 1 Model B  0.7         512.0       1           0           2014-07-01
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 11:58:34 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2022-08-05 20:30:04 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 11:58:34 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2022-08-05 20:30:04 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								count(*)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								----------
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								5
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 11:58:34 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								>  Any value that is not a number should be in single-quotes, never double quotes
 
							 
						 
					
						
							
								
									
										
										
										
											2022-08-05 20:30:04 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								## Main commands
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								There are obviously many SQL commands but most standard CRUD actions can be executed with a small number of commands:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-08-16 11:58:34 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								-  `SELECT` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-  `UPDATE` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-  `CREATE` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-  `INSERT` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								-  `DELETE`