Database schema:

CREATE TABLE "Award" (
	award_id INTEGER -- example: ['326', '325'],
	organization TEXT -- example: ['Annie Awards', 'Primetime Emmy Awards'],
	year INTEGER -- example: ['2010', '2009'],
	award_category TEXT -- example: ['Primetime Emmy', 'Annie'],
	award TEXT -- example: ['Outstanding Voice-Over Performance', 'Outstanding Animated Program (For Programming Less'],
	person TEXT -- example: ['Dan Castellaneta', 'Hank Azaria'],
	role TEXT -- example: ['co-executive producer', 'executive producer'],
	episode_id TEXT -- example: ['S20-E18', 'S20-E16'],
	season TEXT, 
	song TEXT, 
	result TEXT -- example: ['Nominee', 'Winner'],
	PRIMARY KEY (award_id), 
	CONSTRAINT fk_award_person FOREIGN KEY(person) REFERENCES "Person" (name),
	CONSTRAINT fk_award_episode FOREIGN KEY(episode_id) REFERENCES "Episode" (episode_id)
)


CREATE TABLE "Character_Award" (
	award_id INTEGER -- example: ['326', '325'],
	character TEXT -- example: ['Moe Szyslak', 'Homer Simpson'],
	CONSTRAINT fk_character_award_award FOREIGN KEY(award_id) REFERENCES "Award" (award_id)
)


CREATE TABLE "Credit" (
	episode_id TEXT -- example: ['S20-E13', 'S20-E10'],
	category TEXT -- example: ['Casting Department', 'Animation Department'],
	person TEXT -- example: ['Adam Kuhlman', 'Bonita Pietila'],
	role TEXT -- example: ['additional timer', 'casting'],
	credited TEXT -- example: ['false', 'true'],
	CONSTRAINT fk_credit_episode FOREIGN KEY(episode_id) REFERENCES "Episode" (episode_id),
	CONSTRAINT fk_credit_person FOREIGN KEY(person) REFERENCES "Person" (name)
)


CREATE TABLE "Episode" (
	episode_id TEXT -- example: ['S20-E1', 'S20-E10'],
	season INTEGER -- example: ['20'],
	episode INTEGER -- example: ['2', '1'],
	number_in_series INTEGER -- example: ['421', '422'],
	title TEXT -- example: ['Sex, Pies and Idiot Scrapes', 'Lost Verizon'],
	summary TEXT -- example: ['Bart gets in trouble with Marge after she finds ou', 'Homer and Ned go into business together as bounty '],
	air_date TEXT -- example: ['2008-09-28', '2008-10-05'],
	episode_image TEXT -- example: ['https://m.media-amazon.com/images/M/MV5BMjMyNzU4OD', 'https://m.media-amazon.com/images/M/MV5BMTYwMzk2Nj'],
	rating REAL -- example: ['7.2', '7.0'],
	votes INTEGER -- example: ['1055', '1192'],
	PRIMARY KEY (episode_id)
)


CREATE TABLE "Keyword" (
	episode_id TEXT -- example: ['S20-E1', 'S20-E10'],
	keyword TEXT -- example: ['1930s to 2020s', 'erotic bakery'],
	PRIMARY KEY (episode_id, keyword), 
	CONSTRAINT fk_keyword_episode FOREIGN KEY(episode_id) REFERENCES "Episode" (episode_id)
)


CREATE TABLE "Person" (
	name TEXT -- example: ['Adam Greeley', 'Adam Klein'],
	birthdate TEXT -- example: ['1975-03-30', '1963-05-04'],
	birth_name TEXT -- example: ['Marc Edward Wilmore', 'Valentina Lantigua'],
	birth_place TEXT -- example: ['Brooklyn', 'USA'],
	birth_region TEXT -- example: ['New York', 'California'],
	birth_country TEXT -- example: ['USA', 'Canada'],
	height_meters REAL -- example: ['1.75', '1.85'],
	nickname TEXT -- example: ['Jim', 'Doofus'],
	PRIMARY KEY (name)
)


CREATE TABLE "Vote" (
	episode_id TEXT -- example: ['S20-E1', 'S20-E10'],
	stars INTEGER -- example: ['2', '3'],
	votes INTEGER -- example: ['20', '16'],
	percent REAL -- example: ['1.3', '1.7'],
	CONSTRAINT fk_vote_episode FOREIGN KEY(episode_id) REFERENCES "Episode" (episode_id)
)

Question: Among the episodes which have star score less than 8, how many episodes were aired in 2009?
Evidence: star score less than 8 refers to stars < 8; aired in 2009 refers to air_date LIKE '2009%'

SQL:
SELECT COUNT(DISTINCT T2.episode_id) FROM Episode AS T1 INNER JOIN Vote AS T2 ON T2.episode_id = T1.episode_id WHERE strftime('%Y', T1.air_date) = '2009' AND T2.stars < 8;