SQL injection vs cross-site scripting (XSS): Security+ explained
Both are injection: untrusted input is treated as code instead of data. In SQL injection the database runs the input as part of a query. In cross-site scripting the input is served back in a web page and runs as script in other users' browsers.
SQL injection
CWE-89 describes a product that builds an SQL command from external input without neutralizing special elements, so the input is interpreted as SQL instead of ordinary data. The attack runs on the server, against the database.
CWE-89's most effective fix is to separate data from code with prepared statements, parameterized queries or stored procedures, without building query strings dynamically inside them. Allowlist input validation adds depth but does not reliably stop SQL injection alone. Running the application with a low-privilege database account limits the damage.
Cross-site scripting
CWE-79 is a child of CWE-74, Injection. The input ends up in a web page served to other users, and runs in their browsers. CWE-79 names three types. Reflected XSS: the server reads data from the HTTP request and reflects it in the response. Stored XSS: the server saves the data, for example in a forum or database, and later puts it into pages for other visitors. DOM-based XSS: the client-side script itself inserts the data into the page.
The main fix is output encoding for the context the data lands in: HTML body, attribute, URL, JavaScript or CSS. Input validation is defense in depth. Setting the HttpOnly flag on a session cookie keeps it from page scripts, but CWE-79 says that is not a complete solution.
Other injection
CWE-78, OS command injection, is also a child of CWE-74. A page that passes a user-supplied hostname to a ping command, where a semicolon lets a second command run, is command injection. Security+ often groups SQL injection, XSS and command injection under the single answer "injection".
Where the exam catches people
Believing XSS attacks the server's database: the script runs in other users' browsers; SQL injection is the one that runs in the database.
Believing input validation is the main fix for SQL injection: CWE-89 names parameterized queries as most effective and says validation alone is not reliable.
Believing XSS is not injection: CWE-79 is a child of CWE-74, Injection.
Believing HttpOnly cookies prevent XSS: they only keep the session cookie from scripts, and CWE-79 calls this incomplete.
Believing stored and reflected XSS differ in payload: they differ in whether the server saves the input or echoes it from the current request.