In: Computer Science
List the product name and description of every product with a list price higher than $500 that has either the word “humbuck” or “fret” in the description. Use a regular expression to do this.
The SQL query with regular expression can be given as follows
SELECT productname , description FROM myguitarshop WHERE listprice > '$500' AND ( description REGEXP [[:<:]] humback OR description REGEXP [[:<]] fret) ;
The above SQL query can show all the products from the table myguitarshop with product name and description with price above 500 dollars. And with the condition which the description should have words "humback" or "fret".
The explanation for each part of the query is as follows:
SELECT productname, description from myguitarshop
From syntax
SELECT columnname1, columnname2 from TABLE
This part of the query selects the two columns productname and description from the table myguitarshop.
WHERE listprice > '$500'
WHERE condition
It selects the contents from the table where the listprice mentioned in the table is above the value $100
AND ( )
The AND is a SQL query that is used to join or make more than one condition to work in the query. Here the AND is used to join the two part of the query
description REGEXP [[:<:]] humback OR description REGEXP [[:<:]] fret
This part of the query contains the regular expression
WHERE columnname REGEXP [[:<]] characters
The above part of query from this syntax used by REGEXP returns the contents where the description has the words humback or fret. The OR SQL operator is used to select the description with either of these words.